TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

ad21ea876bd455dbebeec26e6b2ec765541a46b0.svn-base (3853B)


      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 		for (int i = 0; i < (int)act.GetHats().size(); i++)
     51 		{
     52 			if (((Hat*)(act.GetHats().at(i)))->IsEnabled())
     53 			{
     54 				DrawActor(ren, *act.GetHats().at(i), cam);
     55 			}
     56 		}
     57 	}
     58 	else
     59 	{
     60 		//gCons->ConsPrintf("Actor frame is null\n");
     61 	}
     62 	return ScreenPos;
     63 }
     64 
     65 bool DrawRect(SDL_Renderer* mRen, SDL_Rect WrldPos, Camera& mCam)
     66 {
     67 	SDL_Rect ScreenPos;
     68 	ScreenPos.h = (int)((float)(WrldPos.h)*((float)(SCREEN_H) / (float)(mCam.GetCamView()->h)));
     69 	ScreenPos.w = (int)((float)(WrldPos.w)*((float)(SCREEN_W) / (float)(mCam.GetCamView()->w)));
     70 
     71 	ScreenPos.x = (int)((float)(WrldPos.x - mCam.GetCamX())*((float)(SCREEN_W) / (float)(mCam.GetCamView()->w)));
     72 	ScreenPos.y = (int)((float)(WrldPos.y - mCam.GetCamY())*((float)(SCREEN_H) / (float)(mCam.GetCamView()->h)));
     73 
     74 	SDL_Point pts[5];
     75 	pts[0].x = ScreenPos.x;
     76 	pts[0].y = ScreenPos.y;
     77 	pts[1].x = ScreenPos.x + ScreenPos.w;
     78 	pts[1].y = ScreenPos.y;
     79 	pts[2].x = ScreenPos.x + ScreenPos.w;
     80 	pts[2].y = ScreenPos.y + ScreenPos.h;
     81 	pts[3].x = ScreenPos.x;
     82 	pts[3].y = ScreenPos.y + ScreenPos.h;
     83 	pts[4].x = ScreenPos.x;
     84 	pts[4].y = ScreenPos.y;
     85 
     86 	SDL_RenderDrawLines(mRen, pts, 5);
     87 
     88 	return false;
     89 }
     90 
     91 bool DrawText(SDL_Renderer* ren, TTF_Font* font, char* text, SDL_Color Text_c, SDL_Color Back_c)
     92 {
     93 	const char* error;
     94 
     95 	SDL_Surface* LineSur = TTF_RenderText_Shaded(font, text, Text_c, Back_c);
     96 	if (!LineSur)
     97 	{
     98 		error = SDL_GetError();
     99 	}
    100 
    101 	SDL_SetColorKey(LineSur, SDL_TRUE, SDL_MapRGB(LineSur->format, 0xFF, 0x00, 0xFF));
    102 
    103 	SDL_Texture* LineTex = SDL_CreateTextureFromSurface(ren, LineSur);
    104 	if (!LineTex)
    105 	{
    106 		error = SDL_GetError();
    107 	}
    108 	SDL_FreeSurface(LineSur);
    109 
    110 	SDL_Rect TextRect;
    111 
    112 	if (SDL_QueryTexture(LineTex, NULL, NULL, &TextRect.w, &TextRect.h))
    113 	{
    114 		error = SDL_GetError();
    115 	}
    116 
    117 	SDL_Rect LinePosRect;
    118 
    119 	LinePosRect.w = TextRect.w;
    120 	LinePosRect.h = TextRect.h;
    121 	LinePosRect.x = 0;
    122 	LinePosRect.y = 0;
    123 	//Why comment?
    124 //	if (SDL_RenderCopy(ren, LineTex, &mTextRect, &mLinePosRect))
    125 //	{
    126 //		error = SDL_GetError();
    127 //	}
    128 	SDL_DestroyTexture(LineTex);
    129 
    130 	return false;
    131 }