a94ff71fe029d9b5e3f73f615c2837ba2bef4114.svn-base (3921B)
1 #include"Utils.h" 2 #include"Console.h" 3 #include "MiscDraw.h" 4 5 SDL_Rect GetScreenPos(Actor& act, Camera cam) 6 { 7 SDL_Rect ScreenPos; 8 ScreenPos.h = (int)floor((float)(act.GetDrawHeight()) * ((float)(SCREEN_H) / (float)(cam.GetCamView()->h))); 9 ScreenPos.w = (int)floor((float)(act.GetDrawWidth()) * ((float)(SCREEN_W) / (float)(cam.GetCamView()->w))); 10 11 ScreenPos.x = (int)floor((float)(act.GetDrawX() + act.GetX() - cam.GetCamX())*((float)(SCREEN_W) / (float)(cam.GetCamView()->w))); 12 ScreenPos.y = (int)floor((float)(act.GetDrawY() + act.GetY() - cam.GetCamY())*((float)(SCREEN_H) / (float)(cam.GetCamView()->h))); 13 14 return ScreenPos; 15 } 16 17 bool DrawParallax(SDL_Renderer* ren, SDL_Texture* img, Camera cam, float scale, int worldH, int worldW) 18 { 19 int imgH; 20 int imgW; 21 SDL_QueryTexture(img, NULL, NULL, &imgW, &imgH); 22 23 SDL_Rect clip; 24 clip.h = (int)(cam.GetCamView()->h * scale); 25 clip.w = (int)(cam.GetCamView()->w * scale); 26 clip.x = (int)(cam.GetCamX() / ( (worldW - cam.GetCamView()->w) / ( imgW - (cam.GetCamView()->w)*scale) )); 27 clip.y = (int)(cam.GetCamY() / ( (worldH - cam.GetCamView()->h) / ( imgH - (cam.GetCamView()->h)*scale) )); 28 29 // gCons->ConsPrintf("para bound x: %i - %i\n", clip.x, clip.w + clip.x); 30 // gCons->ConsPrintf("para bound y: %i - %i\n", clip.y, clip.h + clip.y); 31 32 SDL_RenderCopy(ren, img, &clip, NULL); 33 34 return true; 35 } 36 37 SDL_Rect DrawActor(SDL_Renderer* ren, Actor& act, Camera cam) 38 { 39 SDL_Rect ScreenPos = GetScreenPos(act, cam); 40 41 Frame* RenderFrame = act.GetFrame(); 42 43 if (RenderFrame != NULL) 44 { 45 SDL_RendererFlip flip; 46 flip = (SDL_RendererFlip)act.GetDir(); //this is why LEFT and RIGHT have thier specific definitions 47 SDL_SetTextureAlphaMod(RenderFrame->FrameSource, act.GetOpacity()); 48 49 SDL_RenderCopyEx(ren, RenderFrame->FrameSource, &(RenderFrame->FrameRect), &ScreenPos, 0, NULL, flip); 50 Node** hat_array = act.GetHats()->Dump(); 51 for (int i = 0; i < act.GetHats()->Size(); i++) 52 { 53 // if (((Hat*)( hat_array[i]->GetData() ))->IsEnabled()) 54 // { 55 // DrawActor(ren, *(Actor*)(hat_array[i]->GetData()), cam); 56 // } 57 } 58 } 59 else 60 { 61 //gCons->ConsPrintf("Actor frame is null\n"); 62 } 63 return ScreenPos; 64 } 65 66 bool DrawRect(SDL_Renderer* mRen, SDL_Rect WrldPos, Camera& mCam) 67 { 68 SDL_Rect ScreenPos; 69 ScreenPos.h = (int)((float)(WrldPos.h)*((float)(SCREEN_H) / (float)(mCam.GetCamView()->h))); 70 ScreenPos.w = (int)((float)(WrldPos.w)*((float)(SCREEN_W) / (float)(mCam.GetCamView()->w))); 71 72 ScreenPos.x = (int)((float)(WrldPos.x - mCam.GetCamX())*((float)(SCREEN_W) / (float)(mCam.GetCamView()->w))); 73 ScreenPos.y = (int)((float)(WrldPos.y - mCam.GetCamY())*((float)(SCREEN_H) / (float)(mCam.GetCamView()->h))); 74 75 SDL_Point pts[5]; 76 pts[0].x = ScreenPos.x; 77 pts[0].y = ScreenPos.y; 78 pts[1].x = ScreenPos.x + ScreenPos.w; 79 pts[1].y = ScreenPos.y; 80 pts[2].x = ScreenPos.x + ScreenPos.w; 81 pts[2].y = ScreenPos.y + ScreenPos.h; 82 pts[3].x = ScreenPos.x; 83 pts[3].y = ScreenPos.y + ScreenPos.h; 84 pts[4].x = ScreenPos.x; 85 pts[4].y = ScreenPos.y; 86 87 SDL_RenderDrawLines(mRen, pts, 5); 88 89 return false; 90 } 91 92 bool DrawText(SDL_Renderer* ren, TTF_Font* font, char* text, SDL_Color Text_c, SDL_Color Back_c) 93 { 94 const char* error; 95 96 SDL_Surface* LineSur = TTF_RenderText_Shaded(font, text, Text_c, Back_c); 97 if (!LineSur) 98 { 99 error = SDL_GetError(); 100 } 101 102 SDL_SetColorKey(LineSur, SDL_TRUE, SDL_MapRGB(LineSur->format, 0xFF, 0x00, 0xFF)); 103 104 SDL_Texture* LineTex = SDL_CreateTextureFromSurface(ren, LineSur); 105 if (!LineTex) 106 { 107 error = SDL_GetError(); 108 } 109 SDL_FreeSurface(LineSur); 110 111 SDL_Rect TextRect; 112 113 if (SDL_QueryTexture(LineTex, NULL, NULL, &TextRect.w, &TextRect.h)) 114 { 115 error = SDL_GetError(); 116 } 117 118 SDL_Rect LinePosRect; 119 120 LinePosRect.w = TextRect.w; 121 LinePosRect.h = TextRect.h; 122 LinePosRect.x = 0; 123 LinePosRect.y = 0; 124 //Why comment? 125 // if (SDL_RenderCopy(ren, LineTex, &mTextRect, &mLinePosRect)) 126 // { 127 // error = SDL_GetError(); 128 // } 129 SDL_DestroyTexture(LineTex); 130 131 return false; 132 }