]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Added texture support and textures
authordrumsetmonkey <abelleisle@roadrunner.com>
Sun, 4 Oct 2015 23:35:27 +0000 (19:35 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Sun, 4 Oct 2015 23:35:27 +0000 (19:35 -0400)
assets/NPC.png [new file with mode: 0644]
assets/player.png [new file with mode: 0644]
include/common.h
include/entities.h
src/common.cpp [new file with mode: 0644]
src/entities.cpp
src/main.cpp
src/ui.cpp

diff --git a/assets/NPC.png b/assets/NPC.png
new file mode 100644 (file)
index 0000000..6f2fbca
Binary files /dev/null and b/assets/NPC.png differ
diff --git a/assets/player.png b/assets/player.png
new file mode 100644 (file)
index 0000000..4dbe772
Binary files /dev/null and b/assets/player.png differ
index 9f29fbb26217a19a7adfc76dd01acc9ad2575674..b283ec88e53f902291a304adaa47b6102b45e5db 100644 (file)
@@ -41,6 +41,8 @@ enum GENDER{
 template<typename T, size_t N>                                         //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;
 
index b58d56946bd088798feba05e74aa03b3ee9d80d4..d4df811b4a18af9ba86d2c8102aa4527e7b03a5d 100644 (file)
@@ -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 (file)
index 0000000..124d999
--- /dev/null
@@ -0,0 +1,41 @@
+#include <common.h>
+
+/*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
index 9d7ce95ddb863587c0144614b0400eb461b431c0..429e3e58b5b8ea864d34423cc538e36114a22760 100644 (file)
@@ -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;i<tempN;i++){
index 02ddc3cd72b7e746ab781c9e6c137c3b8bb3e27f..f4e39435731d522c323b0c8743e51eaf34d36fd5 100644 (file)
@@ -152,6 +152,7 @@ void render(){
        glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack
        glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_MODELVIEW mode
        glPushMatrix();                                                                 //basically here we put a blank canvas (new matrix) on the screen to draw on
+       glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );
        glClear(GL_COLOR_BUFFER_BIT);                                   //clear the matrix on the top of the stack
 
        /**************************
index 8b62aab86bab15a3170276ca48f18d99e49b6d3e..80ecc69eaca371cec63d7df22b595a4fb2309de9 100644 (file)
@@ -187,11 +187,15 @@ namespace ui {
                                if(SDL_KEY==SDLK_a){                                                                                            // Move left
                                        left=true;
                                        player->vel.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;