diff --git a/SDL2_ttf.dll b/SDL2_ttf.dll new file mode 100644 index 0000000..2d8929e Binary files /dev/null and b/SDL2_ttf.dll differ diff --git a/libfreetype-6.dll b/libfreetype-6.dll new file mode 100644 index 0000000..6b19b3c Binary files /dev/null and b/libfreetype-6.dll differ diff --git a/overworld.map b/overworld.map index 2c6783c..87fa7a4 100644 --- a/overworld.map +++ b/overworld.map @@ -1,6 +1 @@ -01 07 --08 07 02 02 -04 09 01 02 -06 09 01 02 -00 11 16 01 -20 11 16 01 \ No newline at end of file +1 7 5 8 7 2 2 4 9 1 2 6 9 1 2 0 11 16 1 20 11 16 1 \ No newline at end of file diff --git a/overworld2.map b/overworld2.map new file mode 100644 index 0000000..2c6783c --- /dev/null +++ b/overworld2.map @@ -0,0 +1,6 @@ +01 07 +-08 07 02 02 +04 09 01 02 +06 09 01 02 +00 11 16 01 +20 11 16 01 \ No newline at end of file diff --git a/pangolin.ttf b/pangolin.ttf new file mode 100644 index 0000000..e0031b0 Binary files /dev/null and b/pangolin.ttf differ diff --git a/platform-test.exe b/platform-test.exe index 0d0edf8..8f154f9 100644 Binary files a/platform-test.exe and b/platform-test.exe differ diff --git a/source/core.cpp b/source/core.cpp index 2aad906..effbb2a 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -27,16 +27,10 @@ int Core::coreInit(){ //-1 = quit, 0 = Menu, 1 = playing, while(gamestate != -1){ - switch(gamestate){ - case 0: - gamestate = menu(gRenderer); - break; - case 1: - gamestate = maps.map("overworld"); - break; - /*case 2: - gamestate = maps.map2();*/ - } + if(gamestate == 0) gamestate = menu(gRenderer); + else if(gamestate == 1) gamestate = maps.map("overworld"); + else gamestate = -1; + } close(); diff --git a/source/entity/player.cpp b/source/entity/player.cpp index 3353dbf..cf8ba44 100644 --- a/source/entity/player.cpp +++ b/source/entity/player.cpp @@ -2,22 +2,25 @@ #include"player.h" -Player::Player(){ +Player::Player(int x,int y, int lvlH, SDL_Renderer* render){ szW = 40; szH = 40; + rect.w = szW; rect.h = szH; + power = 0; ground = false; -}; -void Player::set(int x,int y, int lvlH, SDL_Renderer* render){ levelHeight = lvlH; posX = x; posY = y; + initPosX = x; + initPosY = y; + renderer = render; ply.setRenderer(renderer); @@ -47,11 +50,21 @@ int Player::print(int cameraX){ topCollision = false; //Check if the player has fell out of the world (not horizontally, sadly). - if(posY >= levelHeight) return 1; + if(posY >= levelHeight) die(); return 0; }; +void Player::die(){ + posX = initPosX; + posY = initPosY; + + velocityX = 0; + velocityY = 0; + + power = 0; +} + void Player::loadMedia(){ ply.loadTexture("textures/player.png"); diff --git a/source/entity/player.h b/source/entity/player.h index 0ef90c9..75b026e 100644 --- a/source/entity/player.h +++ b/source/entity/player.h @@ -14,13 +14,13 @@ class Entity; class Player: public Entity{ public: - Player(); - void set(int x,int y,int lvlH,SDL_Renderer* render); + Player(int x,int y,int lvlH,SDL_Renderer* render); int print(int cameraX); int check(SDL_Rect rectA,int type); private: void loadMedia(); void move(); + void die(); int intVelX(); SDL_Rect plyFrame[3]; @@ -37,6 +37,7 @@ class Player: public Entity{ float velocityY = 0; //int posX, posY; + int initPosX, initPosY; int oldPosX, oldPosY; int power; int levelHeight; diff --git a/source/maps.cpp b/source/maps.cpp index 4e54366..5ce1560 100644 --- a/source/maps.cpp +++ b/source/maps.cpp @@ -16,6 +16,10 @@ Maps::Maps(SDL_Renderer* render){ //Set player default position playerPosX = 1*sz; playerPosY = 1*sz; + + blockTotal = 0; + mapWidth = 0; + mapHeight = 0; }; Maps::~Maps(){ @@ -27,7 +31,7 @@ Maps::~Maps(){ void Maps::loadMap(std::string path){ //Open the map to read it - std::ifstream map(path.c_str()); + std::ifstream map(path.c_str()); //Check if the map is open if(map == NULL) std::cout << "Unable to load map" << std::endl; @@ -39,21 +43,13 @@ void Maps::loadMap(std::string path){ std::cout << playerPosX << " " << playerPosY << std::endl; //Get the total of blocks - blockTotal = std::count(std::istreambuf_iterator(map), std::istreambuf_iterator(), '\n'); + map >> blockTotal; std::cout << blockTotal << std::endl; - map.clear(); - map.seekg(0,map.beg); - map.ignore(std::numeric_limits::max(),'-'); - - // dynamically allocate memory using new - blockRect = new int*[blockTotal]; + blockRect = new int*[blockTotal]; //Build rows for(int i = 0; i < blockTotal; ++i) blockRect[i] = new int[4]; - //Initialize size of the map variables - int mH = 0,mW = 0; - //Load the block information for(int i = 0; i < blockTotal; i++){ for(int j = 0; j < 4; j++){ @@ -61,24 +57,29 @@ void Maps::loadMap(std::string path){ blockRect[i][j] *= 40; std::cout << blockRect[i][j] << " "; } - if(blockRect[i][0]+blockRect[i][2] > mW) mW = blockRect[i][0]+blockRect[i][2]; - if(blockRect[i][1]+blockRect[i][3] > mH) mH = blockRect[i][1]+blockRect[i][3]; + if(blockRect[i][0]+blockRect[i][2] > mapWidth) mapWidth = blockRect[i][0]+blockRect[i][2]; + if(blockRect[i][1]+blockRect[i][3] > mapHeight) mapHeight = blockRect[i][1]+blockRect[i][3]; std::cout << std::endl; } + std::cout << mapWidth << " - " << mapHeight << std::endl; //Very descriptive by itself map.close(); - - //Initialize the player, set its position, pass the map's size and set the renderer - player.set(playerPosX,playerPosY,mH,renderer); }; int Maps::map(std::string mapName){ //Load map loadMap(mapName + ".map"); - int mapHeight = 12*sz; - int mapWidth = 36*sz; + Texture level; + level.setRenderer(renderer); + SDL_Color textColor = {0xFF,0xFF,0xFF}; + level.loadFromRendererText(mapName,textColor); + + SDL_Rect levelRect = {0,0,level.getWidth(),level.getHeight()}; + + //Initialize the player, set its position, pass the map's size and set the renderer + Player player(playerPosX,playerPosY,mapHeight,renderer); //Set the camera and pass the map and screen's dimensions Camera camera(mapWidth,mapHeight,screenWidth,screenHeight); @@ -105,6 +106,8 @@ int Maps::map(std::string mapName){ SDL_SetRenderDrawColor(renderer,0,0,100,0xFF); SDL_RenderClear(renderer); + level.render(&levelRect); + //Update the camera position camera.update(player.getRectangle().x,player.getRectangle().y); diff --git a/source/maps.h b/source/maps.h index 94bbdfb..461bf7a 100644 --- a/source/maps.h +++ b/source/maps.h @@ -9,6 +9,7 @@ #include"camera.h" #include #include +#include"texture.h" class Maps{ public: @@ -19,12 +20,12 @@ class Maps{ void loadMap(std::string path); SDL_Renderer* renderer; - Player player; int blockTotal; int** blockRect; int sz; int screenWidth, screenHeight; + int mapWidth, mapHeight; int playerPosX, playerPosY; const Uint8* currentKeyStates; }; diff --git a/source/menu.cpp b/source/menu.cpp index b8f51dc..dac4f43 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -29,6 +29,25 @@ int menu(SDL_Renderer* renderer){ button[i].h = btnHeight; } + Texture play; + play.setRenderer(renderer); + SDL_Color textColor = {0xFF,0xFF,0xFF}; + play.loadFromRendererText("Play",textColor); + + SDL_Rect playRect = {button[0].x+(button[0].w/2)-play.getWidth()/2,button[0].y,play.getWidth(),play.getHeight()}; + + Texture exit; + exit.setRenderer(renderer); + exit.loadFromRendererText("Quit",textColor); + + SDL_Rect exitRect = {button[2].x+(button[2].w/2)-exit.getWidth()/2,button[2].y,exit.getWidth(),exit.getHeight()}; + + Texture options; + options.setRenderer(renderer); + options.loadFromRendererText("Options",textColor); + + SDL_Rect optionsRect = {button[1].x+(button[1].w/2)-options.getWidth()/2,button[1].y,options.getWidth(),options.getHeight()}; + int select = 0; bool wait = 0; const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); @@ -66,6 +85,11 @@ int menu(SDL_Renderer* renderer){ SDL_RenderFillRect(renderer,&button[i]); } + + play.render(&playRect); + exit.render(&exitRect); + options.render(&optionsRect); + if(currentKeyStates[SDL_SCANCODE_SPACE]){ if(select == 0) return 1; else if(select == 1) return 0; diff --git a/source/texture.cpp b/source/texture.cpp index 0e2f62a..225fc54 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -10,6 +10,9 @@ Texture::Texture(){ //Initialize the SDL_Image library IMG_Init(IMG_INIT_PNG); + if(TTF_Init() == -1) std::cout << TTF_GetError() << std::endl; + + font = TTF_OpenFont("pangolin.ttf",30); }; void Texture::setRenderer(SDL_Renderer* render){ @@ -20,6 +23,9 @@ void Texture::setRenderer(SDL_Renderer* render){ Texture::~Texture(){ //Deallocate the texture when the class closes free(); + + IMG_Quit(); + TTF_Quit(); }; void Texture::loadTexture(std::string path){ @@ -50,6 +56,39 @@ void Texture::loadTexture(std::string path){ } }; +bool Texture::loadFromRendererText(std::string textureText, SDL_Color textColor){ + //Get rid of preexisting texture + free(); + + //Render text surface + SDL_Surface* textSurface = TTF_RenderText_Solid( font, textureText.c_str(), textColor ); + if( textSurface == NULL ) + { + std::cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl; + } + else + { + //Create texture from surface pixels + texture = SDL_CreateTextureFromSurface( renderer, textSurface ); + if( texture == NULL ) + { + std::cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << std::endl; + } + else + { + //Get image dimensions + szW = textSurface->w; + szH = textSurface->h; + } + + //Get rid of old surface + SDL_FreeSurface( textSurface ); + } + + //Return success + return texture != NULL; +} + void Texture::free(){ //Free texture if it exists if(texture != NULL){ diff --git a/source/texture.h b/source/texture.h index f627868..b599a06 100644 --- a/source/texture.h +++ b/source/texture.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -13,6 +14,7 @@ class Texture{ Texture(); ~Texture(); void loadTexture(std::string path); + bool loadFromRendererText(std::string textureText, SDL_Color textColor); void free(); void render(SDL_Rect* quad,SDL_Rect* frame); void render(SDL_Rect* quad); @@ -22,6 +24,7 @@ class Texture{ private: SDL_Texture* texture; SDL_Renderer* renderer; + TTF_Font *font = NULL; int szW, szH; };