]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
rip game
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 23 Mar 2016 12:49:28 +0000 (08:49 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 23 Mar 2016 12:49:28 +0000 (08:49 -0400)
Changelog
main.cpp
src/ui.cpp
src/world.cpp

index 546ded47519d83e3810fb2a7ddc6c52b49f7f047..00709fd8898783c01e3010f4c8669756fb0ca8b5 100644 (file)
--- a/Changelog
+++ b/Changelog
        - game can run on *BSD systems..
        - fixed abort bug on exit
        - added light that follows player
+
+3/23/2016:
+==========
+
+       - flickery lights
+       - ui font loading is smooth
+       - entities can be flung
index 0b3984e9673789e0e068cf301ee5f7889d5b1219..210f36c229080a9d3743ff9ce53070f0bdefd85b 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -77,7 +77,7 @@ World *currentWorld        = NULL,
  * The player object.
  */
 
-Player         *player;
+Player *player;
 
 /**
  * TODO
index 458e797ae6923073399fc84ac856e6747bbe6e9d..8e5a304d55b268c9bb92e1e69f55c22a6eaa1bbb 100644 (file)
@@ -33,8 +33,8 @@ extern bool gameRunning;
 extern unsigned int tickCount;
 
 /*
- *     Freetype variables, and a GLuint for referencing rendered letters.
-*/
+ *     Freetype variables
+ */
 
 static FT_Library   ftl;
 static FT_Face      ftf;
@@ -45,8 +45,16 @@ typedef struct {
        vec2 ad;
 } FT_Info;
 
-static std::vector<FT_Info> ftdat ( 93, { { 0, 0 }, { 0, 0 }, { 0, 0 } } );
-static std::vector<GLuint>  ftex  ( 93, 0 );
+static std::vector<FT_Info> ftdat16 ( 93, { { 0, 0 }, { 0, 0 }, { 0, 0 } } );
+static std::vector<GLuint>  ftex16  ( 93, 0 );
+static bool ft16loaded = false;
+
+static std::vector<FT_Info> ftdat24 ( 93, { { 0, 0 }, { 0, 0 }, { 0, 0 } } );
+static std::vector<GLuint>  ftex24  ( 93, 0 );
+static bool ft24loaded = false;
+
+static auto *ftdat = &ftdat16;
+static auto *ftex  = &ftex16;
 
 static unsigned char fontColor[4] = {255,255,255,255};
 
@@ -97,10 +105,61 @@ void Menu::gotoParent(){
 }
 
 void Menu::gotoChild(){
-       if(child == NULL){
-               currentMenu = NULL;
-       }else{
-               currentMenu = child;
+       currentMenu = child;
+}
+
+void loadFontSize( unsigned int size, std::vector<GLuint> &tex, std::vector<FT_Info> &dat )
+{
+       FT_Set_Pixel_Sizes(ftf,0,size);
+
+       /*
+        *      Pre-render 'all' the characters.
+       */
+
+       glDeleteTextures( 93, tex.data() );
+       glGenTextures( 93, tex.data() );                //      Generate new texture name/locations?
+
+       for(char i=33;i<126;i++){
+
+               /*
+                *      Load the character from the font family file.
+               */
+
+               if ( FT_Load_Char ( ftf, i, FT_LOAD_RENDER ) )
+                       UserError( "Error! Unsupported character " + i );
+
+               /*
+                *      Transfer the character's bitmap (?) to a texture for rendering.
+               */
+
+               glBindTexture(GL_TEXTURE_2D,tex[i-33]);
+               glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S         ,GL_CLAMP_TO_EDGE);
+               glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T         ,GL_CLAMP_TO_EDGE);
+               glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER     ,GL_LINEAR               );
+               glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER     ,GL_LINEAR               );
+               glPixelStorei(GL_UNPACK_ALIGNMENT,1);
+
+               /*
+                *      The just-created texture will render red-on-black if we don't do anything to it, so
+                *      here we create a buffer 4 times the size and transform the texture into an RGBA array,
+                *      making it white-on-black.
+               */
+
+
+               std::vector<uint32_t> buf ( ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows, 0xFFFFFFFF );
+
+               for( unsigned int j = buf.size(); j--; )
+                       buf[j] ^= !ftf->glyph->bitmap.buffer[j] ? buf[j] : 0;
+
+               dat[i - 33].wh.x = ftf->glyph->bitmap.width;
+               dat[i - 33].wh.y = ftf->glyph->bitmap.rows;
+               dat[i - 33].bl.x = ftf->glyph->bitmap_left;
+               dat[i - 33].bl.y = ftf->glyph->bitmap_top;
+               dat[i - 33].ad.x = ftf->glyph->advance.x >> 6;
+               dat[i - 33].ad.y = ftf->glyph->advance.y >> 6;
+
+               glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, ftf->glyph->bitmap.width, ftf->glyph->bitmap.rows,
+                                 0, GL_RGBA, GL_UNSIGNED_BYTE, buf.data() );
        }
 }
 
@@ -158,7 +217,7 @@ namespace ui {
                        std::cout<<"Error! Couldn't initialize freetype."<<std::endl;
                        abort();
                }
-               fontSize = 0;
+
 #ifdef DEBUG
                DEBUG_printf("Initialized FreeType2.\n",NULL);
 #endif // DEBUG
@@ -166,6 +225,8 @@ namespace ui {
                battleStart = Mix_LoadWAV("assets/sounds/frig.wav");
                sanic = Mix_LoadWAV("assets/sounds/sanic.wav");
                //Mix_Volume(1,50);
+
+               fontSize = 0;
        }
 
        void destroyFonts(void){
@@ -189,6 +250,8 @@ namespace ui {
 #ifdef DEBUG
                DEBUG_printf("Using font %s\n",ttf);
 #endif // DEBUG
+               ft16loaded = false;
+               ft24loaded = false;
        }
 
        /*
@@ -196,63 +259,21 @@ namespace ui {
        */
 
        void setFontSize(unsigned int size){
-               mtx.lock();
-               unsigned int i,j;
-
-               fontSize=size;
-               FT_Set_Pixel_Sizes(ftf,0,fontSize);
-
-               /*
-                *      Pre-render 'all' the characters.
-               */
-
-               glDeleteTextures(93,ftex.data());       //      delete[] any already-rendered textures
-               glGenTextures(93,ftex.data());          //      Generate new texture name/locations?
-
-               for(i=33;i<126;i++){
-
-                       /*
-                        *      Load the character from the font family file.
-                       */
-
-                       if(FT_Load_Char(ftf,i,FT_LOAD_RENDER)){
-                               std::cout<<"Error! Unsupported character "<<(char)i<<" ("<<i<<")."<<std::endl;
-                               abort();
+               if ( size == 16 ) {
+                       if( !ft16loaded ) {
+                               loadFontSize( fontSize = size, ftex16, ftdat16 );
+                               ft16loaded = true;
                        }
-
-                       /*
-                        *      Transfer the character's bitmap (?) to a texture for rendering.
-                       */
-
-                       glBindTexture(GL_TEXTURE_2D,ftex[i-33]);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S         ,GL_CLAMP_TO_EDGE);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T         ,GL_CLAMP_TO_EDGE);
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER     ,GL_LINEAR               );
-                       glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER     ,GL_LINEAR               );
-                       glPixelStorei(GL_UNPACK_ALIGNMENT,1);
-
-                       /*
-                        *      The just-created texture will render red-on-black if we don't do anything to it, so
-                        *      here we create a buffer 4 times the size and transform the texture into an RGBA array,
-                        *      making it white-on-black.
-                       */
-
-
-                       std::vector<uint32_t> buf ( ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows, 0 );
-
-                       for( j = 0; j < buf.size(); j++ )
-                               buf[j] = 0x00FFFFFF | (ftf->glyph->bitmap.buffer[j] ? (0xFF << 24) : 0);
-
-                       ftdat[i-33].wh.x=ftf->glyph->bitmap.width;
-                       ftdat[i-33].wh.y=ftf->glyph->bitmap.rows;
-                       ftdat[i-33].bl.x=ftf->glyph->bitmap_left;
-                       ftdat[i-33].bl.y=ftf->glyph->bitmap_top;
-                       ftdat[i-33].ad.x=ftf->glyph->advance.x>>6;
-                       ftdat[i-33].ad.y=ftf->glyph->advance.y>>6;
-
-                       glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf.data());
+                       ftex = &ftex16;
+                       ftdat = &ftdat16;
+               } else if ( size == 24 ){
+                       if ( !ft24loaded ) {
+                               loadFontSize( fontSize = size, ftex24, ftdat24 );
+                               ft24loaded = true;
+                       }
+                       ftex = &ftex24;
+                       ftdat = &ftdat24;
                }
-               mtx.unlock();
        }
 
        /*
@@ -286,16 +307,16 @@ namespace ui {
                 *      Get the width and height of the rendered character.
                */
 
-               c1={(float)floor(x)+ftdat[c-33].bl.x,
-                   (float)floor(y)+ftdat[c-33].bl.y};
-               c2=ftdat[c-33].wh;
+               c1={(float)floor(x)+(*ftdat)[c-33].bl.x,
+                   (float)floor(y)+(*ftdat)[c-33].bl.y};
+               c2=(*ftdat)[c-33].wh;
 
                /*
                 *      Draw the character:
                */
 
                glEnable(GL_TEXTURE_2D);
-               glBindTexture(GL_TEXTURE_2D,ftex[c-33]);
+               glBindTexture(GL_TEXTURE_2D,(*ftex)[c-33]);
                glPushMatrix();
                glTranslatef(0,-c2.y,0);
                glBegin(GL_QUADS);
@@ -309,7 +330,7 @@ namespace ui {
                glDisable(GL_TEXTURE_2D);
 
                // return the width.
-               return ftdat[c-33].ad;
+               return (*ftdat)[c-33].ad;
        }
 
        /*
@@ -383,7 +404,7 @@ namespace ui {
                                width += fontSize / 2;
                                break;
                        default:
-                               width += ftdat[i].wh.x + fontSize * 0.1f;
+                               width += (*ftdat)[i].wh.x + fontSize * 0.1f;
                                break;
                        }
                } while(s[++i]);
@@ -656,9 +677,9 @@ namespace ui {
                                        }
                                }
                                if(fadeIntensity == 255 || dialogPassive){
-                                       //setFontSize(24);
+                                       setFontSize(24);
                                        putStringCentered(offset.x,offset.y,rtext.c_str());
-                                       //setFontSize(16);
+                                       setFontSize(16);
                                }
                        }else if(dialogMerchant){
                                //static int dispItem;
@@ -857,8 +878,8 @@ namespace ui {
 
        void quitGame(){
                dialogBoxExists = false;
-               currentMenu = NULL;
-               delete[] currentMenu;
+               //delete[] currentMenu;
+               //currentMenu = NULL;
                gameRunning = false;
                updateConfig();
                saveConfig();
@@ -1256,6 +1277,9 @@ EXIT:
                mouse.x = premouse.x + offset.x - ( SCREEN_WIDTH / 2 );
                mouse.y = ( offset.y + SCREEN_HEIGHT / 2 ) - premouse.y;
 
+               static vec2 fr;
+               static Entity *ig;
+
                while(SDL_PollEvent(&e)){
                        switch(e.type){
 
@@ -1270,6 +1294,14 @@ EXIT:
                                premouse.y=e.motion.y;
                                break;
 
+                       case SDL_MOUSEBUTTONUP:
+                               if(ig) {
+                                       ig->vel.x = (fr.x - mouse.x) / 50.0f;
+                                       ig->vel.y = (fr.y - mouse.y) / 50.0f;
+                                       ig = NULL;
+                               }
+                               break;
+
                        // mouse clicks
                        case SDL_MOUSEBUTTONDOWN:
                                // right click advances dialog
@@ -1278,6 +1310,17 @@ EXIT:
                                // left click uses item
                                if ( ( e.button.button & SDL_BUTTON_LEFT ) && !dialogBoxExists )
                                        player->inv->usingi = true;
+
+                               for ( auto &e : currentWorld->entity ) {
+                                       if( mouse.x > e->loc.x && mouse.x < e->loc.x + e->width &&
+                                               mouse.y > e->loc.y && mouse.y < e->loc.y + e->height ) {
+                                               e->vel.y = .05;
+                                               fr = mouse;
+                                               ig = e;
+                                               break;
+                                       }
+                               }
+
                                break;
                        case SDL_MOUSEWHEEL:
                                if (e.wheel.y < 0){
@@ -1479,6 +1522,12 @@ EXIT:
 
                                        std::cout << "Took screenshot" << std::endl;
                                        break;
+                               case SDLK_UP:
+                                       player->inv->setSelectionUp();
+                                       break;
+                               case SDLK_DOWN:
+                                       player->inv->setSelectionDown();
+                                       break;
                                default:
                                        break;
                                }
index 1a7322fd400584d2efe0475bfff6b31795aef2f7..a8e6e50fa568ad32b3a2175fb3c1d9d14c463a86 100644 (file)
@@ -314,7 +314,10 @@ update( Player *p, unsigned int delta )
                e->loc.y += e->vel.y * delta;
 
         // dont let structures move?
-               if ( e->type != STRUCTURET && e->canMove ) {
+        if ( e->type == STRUCTURET )
+            e->canMove = true;
+
+               if ( e->canMove ) {
                        e->loc.x += e->vel.x * delta;
 
             // update boolean directions