From 63dc9b399db9faef611c31629e0265b954d74197 Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Sun, 4 Oct 2015 19:35:27 -0400 Subject: [PATCH] Added texture support and textures --- assets/NPC.png | Bin 0 -> 1024 bytes assets/player.png | Bin 0 -> 312 bytes include/common.h | 2 ++ include/entities.h | 2 +- src/common.cpp | 41 +++++++++++++++++++++++++++++++++++ src/entities.cpp | 52 +++++++++++++++++++++++++++++++++++++++------ src/main.cpp | 1 + src/ui.cpp | 8 +++++-- 8 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 assets/NPC.png create mode 100644 assets/player.png create mode 100644 src/common.cpp diff --git a/assets/NPC.png b/assets/NPC.png new file mode 100644 index 0000000000000000000000000000000000000000..6f2fbca8168367f0a49ce1fe0bc80e52f470d2b6 GIT binary patch literal 1024 zcmV+b1poVqP)WFU8GbZ8()Nlj2>E@cM*00U%6L_t(Y$DNi-Z`(!` zg}-@FB-O;SEyqEd0Nu1n(E?c~|2XJ>=%$;lx=Dd{)t!ODO=>`~9gz|_w~IS`$uebn zHk5|LbMEupfeFh1Pg8wP`DX8C>fzP0{VTm!76^ZSM5^#ef;Z0rm;nx00+;dkw?F_s z0jm#}9Hj8~>|5Y#;8$Q3Nfy9GBsv0?Z=T~mT#iTVJ`X(bJMcOhVdEt?bAYl^@DR~NpzdY;(6W2>X22FW229%Xu_5{U z>fvQNAecD-(ioz>uGgYoJc{6lGc^=QfE#+5t%5$C0n-u4B!a6o02|;N;CfvlMXe~_ z!KT)8ub#C@XHp+H3kE9DTEEIYXFwvN;4>HFCh*fa!Ne{fE7O4oa}UlU&SJkx_|f$8 z(2s@D7Zn_Cv;o0ipCGZ2Ic_74YYROLvMxaRs4_J@lr&KW17yjt{SAcFsR5J9)b!NT zf=Lr4vOOuy6HK0vdQ0){<#;6xlAO4^qz!16#JE&#sIti+I?LNWXyPpEYDfgo>@{o5 zivhtuH!XpQnr}?Bx_*G*Z(RfOv~3YDfauXwGpq)REsV9@qyv%QgBY z0=Ll%b62FoR5fjLE?}zw-fD%_LEC%>(T?T7j}YW#6^#`I-iQjF_mH~1-b^Ae0GU_t zJc?8^-Gabv35#s{z-iabMk{8$(f`{x$}H+hEv@=KEJW{K0xDu&VvP#$BvC0Bf%yzR zm+IcB_Du=oQ$n;A@Dhi66XTN%in>>(UGTp=C`55wwmP&b6$qdV9k`7-=q0xXX2u*K zm?s^f z>hyqUe3@1$T|b$I|-6>wc5 zbap%e=F1$L?6j^u&LHzTdX0hFw6_Lk9yZn8Tg7pXEK)wlrUZBooB(!r7};162@ni$ u+S8V@cen{rFZ@Tq&#@2AfnNaG&B6c1K_8YXXrkf(0000|gW!U_%O?XxI14-? ziy0WWg+Z8+Vb&aw9`+JXUsv|~Tuj{77S|u-m;;4mGeaUuobz*YQ}arITm}Z`qSVBa z)D(sC%#sWRcTeAd6une-p!gh57sn8f<8Q-u^ED{&*htNcmHGRhKjeGy#UlbeHc=)M z)nYYXioT0t*rh4OZ|+^kx=_^NTjmmh3rq7vvL8L^<~pTxQ&i~fnx|d=C;X9Qh+nm6 z3e&lk85XO$*K#-T9^gq}-EhZaj{l~wg7&Lt3#EGLuHF1??SitHrUi#jUvqb5 xzrdLB>8H_E`)CvK=n}oP$D1r2zJ6X=bl&=lrMa1dh%+z91D>vaF6*2UngAraZ215H literal 0 HcmV?d00001 diff --git a/include/common.h b/include/common.h index 9f29fbb..b283ec8 100644 --- a/include/common.h +++ b/include/common.h @@ -41,6 +41,8 @@ enum GENDER{ template //this fuction returns the size of any array int eAmt(T (&)[N]){return N;} +GLuint loadTexture(const char *fileName); + extern bool gameRunning; extern unsigned int deltaTime; diff --git a/include/entities.h b/include/entities.h index b58d569..d4df811 100644 --- a/include/entities.h +++ b/include/entities.h @@ -30,7 +30,7 @@ public: char* name; GENDER gender; - unsigned int texture[]; //TODO: ADD TEXTURES + unsigned int texture; //TODO: ADD TEXTURES void spawn(float, float); diff --git a/src/common.cpp b/src/common.cpp new file mode 100644 index 0000000..124d999 --- /dev/null +++ b/src/common.cpp @@ -0,0 +1,41 @@ +#include + +/*SDL_Surface* loadTexture(char* filename){ + SDL_Surface *optimizedSurface = NULL; + SDL_Surface *texture = IMG_Load(filename); + if(texture == NULL){ + std::cout << "Unable to load an image properly from " << filename << "; Error: " << IMG_GetError() << std::endl; + }else{ + optimizedSurface = SDL_ConvertSurface(texture, renderSurface->format, NULL); + if(optimizedSurface == NULL){ + std::cout << "Unable to optimize image properly from " << filename << "; Error: " << IMG_GetError() << std::endl; + } + SDL_FreeSurface(texture); + } + return optimizedSurface; +}*/ + +GLuint loadTexture(const char *fileName){ + SDL_Surface *image = IMG_Load(fileName); + + //SDL_DisplayFormatAlpha(image); + + unsigned object(0); + + glGenTextures(1, &object); + + glBindTexture(GL_TEXTURE_2D, object); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); + + //Free surface + SDL_FreeSurface(image); + + return object; +} \ No newline at end of file diff --git a/src/entities.cpp b/src/entities.cpp index 9d7ce95..429e3e5 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -21,15 +21,51 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o } void Entity::draw(void){ //draws the entities + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D,texture); + glBegin(GL_QUADS); if(type==NPCT){ - if(gender == MALE) - glColor3ub(0,0,100); - else if(gender == FEMALE) + if(gender == MALE){ + glColor3ub(255,255,255); + glTexCoord2i(0,1);glVertex2i(loc.x, loc.y); + glTexCoord2i(1,1);glVertex2i(loc.x + width, loc.y); + glTexCoord2i(1,0);glVertex2i(loc.x + width, loc.y + height); + glTexCoord2i(0,0);glVertex2i(loc.x, loc.y + height); + } + else if(gender == FEMALE){ glColor3ub(255,105,180); - }else if(type==STRUCTURET){ - glColor3ub(100,0,100); + glTexCoord2i(0,1);glVertex2i(loc.x, loc.y); + glTexCoord2i(1,1);glVertex2i(loc.x + width, loc.y); + glTexCoord2i(1,0);glVertex2i(loc.x + width, loc.y + height); + glTexCoord2i(0,0);glVertex2i(loc.x, loc.y + height); + } + } + if(type==PLAYERT){ + if(right==true){ + glTexCoord2i(0,1);glVertex2i(loc.x, loc.y); + glTexCoord2i(1,1);glVertex2i(loc.x + width, loc.y); + glTexCoord2i(1,0);glVertex2i(loc.x + width, loc.y + height); + glTexCoord2i(0,0);glVertex2i(loc.x, loc.y + height); + }if(left==true){ + glRotatef(180.0f, 0.0f, 0.0f, 1.0f); + glScalef(-1.0f,1.0f,1.0f); + glTexCoord2i(0,1);glVertex2i(loc.x, loc.y); + glTexCoord2i(1,1);glVertex2i(loc.x + width, loc.y); + glTexCoord2i(1,0);glVertex2i(loc.x + width, loc.y + height); + glTexCoord2i(0,-0);glVertex2i(loc.x, loc.y + height); + } + + }else{ + glTexCoord2i(0,1);glVertex2i(loc.x, loc.y); + glTexCoord2i(1,1);glVertex2i(loc.x + width, loc.y); + glTexCoord2i(1,0);glVertex2i(loc.x + width, loc.y + height); + glTexCoord2i(0,0);glVertex2i(loc.x, loc.y + height); } - glRectf(loc.x,loc.y,loc.x+width,loc.y+height); + glEnd(); + glDisable(GL_TEXTURE_2D); + glMatrixMode(GL_MODELVIEW); if(near){ ui::setFontSize(14); ui::putText(loc.x,loc.y-ui::fontSize-HLINE/2,"%s",name); @@ -89,6 +125,7 @@ Player::Player(){ //sets all of the player specific traits on object creation alive = true; ground = false; near = true; + texture = loadTexture("assets/player.png"); } void Player::interact(){ //the function that will cause the player to search for things to interact with @@ -104,6 +141,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation alive = true; canMove = true; near = false; + texture = loadTexture("assets/NPC.png"); } void NPC::addAIFunc(int (*func)(NPC *)){ @@ -139,7 +177,7 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure if(type == STRUCTURET){ loc.y=100; width = 20 * HLINE; - height = 16 * HLINE; + height = 20 * HLINE; int tempN = (getRand() % 5 + 2); //amount of villagers that will spawn for(int i=0;ivel.x=-.15; + player->left = true; + player->right = false; currentWorld=currentWorld->goWorldLeft(player); } if(SDL_KEY==SDLK_d){ // Move right right=true; player->vel.x=.15; + player->right = true; + player->left = false; currentWorld=currentWorld->goWorldRight(player); } if(SDL_KEY==SDLK_s && player->ground==2){ @@ -215,8 +219,8 @@ namespace ui { KEYUP */ case SDL_KEYUP: - if(SDL_KEY==SDLK_a)left=false; // Stop the player if movement keys are released - if(SDL_KEY==SDLK_d)right=false; + if(SDL_KEY==SDLK_a){left=false;}// Stop the player if movement keys are released + if(SDL_KEY==SDLK_d){right=false;} if(!left&&!right)player->vel.x=0; if(SDL_KEY==SDLK_LSHIFT)player->speed = 1; -- 2.39.5