Bug fixed and font inclusion

This commit is contained in:
Pòsweg 2017-06-21 22:03:11 +02:00
parent 786fb12847
commit 1da224483b
14 changed files with 120 additions and 41 deletions

BIN
SDL2_ttf.dll Normal file

Binary file not shown.

BIN
libfreetype-6.dll Normal file

Binary file not shown.

View File

@ -1,6 +1 @@
01 07 1 7 5 8 7 2 2 4 9 1 2 6 9 1 2 0 11 16 1 20 11 16 1
-08 07 02 02
04 09 01 02
06 09 01 02
00 11 16 01
20 11 16 01

6
overworld2.map Normal file
View File

@ -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

BIN
pangolin.ttf Normal file

Binary file not shown.

Binary file not shown.

View File

@ -27,16 +27,10 @@ int Core::coreInit(){
//-1 = quit, 0 = Menu, 1 = playing, //-1 = quit, 0 = Menu, 1 = playing,
while(gamestate != -1){ while(gamestate != -1){
switch(gamestate){ if(gamestate == 0) gamestate = menu(gRenderer);
case 0: else if(gamestate == 1) gamestate = maps.map("overworld");
gamestate = menu(gRenderer); else gamestate = -1;
break;
case 1:
gamestate = maps.map("overworld");
break;
/*case 2:
gamestate = maps.map2();*/
}
} }
close(); close();

View File

@ -2,22 +2,25 @@
#include"player.h" #include"player.h"
Player::Player(){ Player::Player(int x,int y, int lvlH, SDL_Renderer* render){
szW = 40; szW = 40;
szH = 40; szH = 40;
rect.w = szW; rect.w = szW;
rect.h = szH; rect.h = szH;
power = 0; power = 0;
ground = false; ground = false;
};
void Player::set(int x,int y, int lvlH, SDL_Renderer* render){
levelHeight = lvlH; levelHeight = lvlH;
posX = x; posX = x;
posY = y; posY = y;
initPosX = x;
initPosY = y;
renderer = render; renderer = render;
ply.setRenderer(renderer); ply.setRenderer(renderer);
@ -47,11 +50,21 @@ int Player::print(int cameraX){
topCollision = false; topCollision = false;
//Check if the player has fell out of the world (not horizontally, sadly). //Check if the player has fell out of the world (not horizontally, sadly).
if(posY >= levelHeight) return 1; if(posY >= levelHeight) die();
return 0; return 0;
}; };
void Player::die(){
posX = initPosX;
posY = initPosY;
velocityX = 0;
velocityY = 0;
power = 0;
}
void Player::loadMedia(){ void Player::loadMedia(){
ply.loadTexture("textures/player.png"); ply.loadTexture("textures/player.png");

View File

@ -14,13 +14,13 @@ class Entity;
class Player: public Entity{ class Player: public Entity{
public: public:
Player(); Player(int x,int y,int lvlH,SDL_Renderer* render);
void set(int x,int y,int lvlH,SDL_Renderer* render);
int print(int cameraX); int print(int cameraX);
int check(SDL_Rect rectA,int type); int check(SDL_Rect rectA,int type);
private: private:
void loadMedia(); void loadMedia();
void move(); void move();
void die();
int intVelX(); int intVelX();
SDL_Rect plyFrame[3]; SDL_Rect plyFrame[3];
@ -37,6 +37,7 @@ class Player: public Entity{
float velocityY = 0; float velocityY = 0;
//int posX, posY; //int posX, posY;
int initPosX, initPosY;
int oldPosX, oldPosY; int oldPosX, oldPosY;
int power; int power;
int levelHeight; int levelHeight;

View File

@ -16,6 +16,10 @@ Maps::Maps(SDL_Renderer* render){
//Set player default position //Set player default position
playerPosX = 1*sz; playerPosX = 1*sz;
playerPosY = 1*sz; playerPosY = 1*sz;
blockTotal = 0;
mapWidth = 0;
mapHeight = 0;
}; };
Maps::~Maps(){ Maps::~Maps(){
@ -39,21 +43,13 @@ void Maps::loadMap(std::string path){
std::cout << playerPosX << " " << playerPosY << std::endl; std::cout << playerPosX << " " << playerPosY << std::endl;
//Get the total of blocks //Get the total of blocks
blockTotal = std::count(std::istreambuf_iterator<char>(map), std::istreambuf_iterator<char>(), '\n'); map >> blockTotal;
std::cout << blockTotal << std::endl; std::cout << blockTotal << std::endl;
map.clear();
map.seekg(0,map.beg);
map.ignore(std::numeric_limits<std::streamsize>::max(),'-');
// dynamically allocate memory using new
blockRect = new int*[blockTotal]; blockRect = new int*[blockTotal];
//Build rows //Build rows
for(int i = 0; i < blockTotal; ++i) blockRect[i] = new int[4]; 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 //Load the block information
for(int i = 0; i < blockTotal; i++){ for(int i = 0; i < blockTotal; i++){
for(int j = 0; j < 4; j++){ for(int j = 0; j < 4; j++){
@ -61,24 +57,29 @@ void Maps::loadMap(std::string path){
blockRect[i][j] *= 40; blockRect[i][j] *= 40;
std::cout << blockRect[i][j] << " "; std::cout << blockRect[i][j] << " ";
} }
if(blockRect[i][0]+blockRect[i][2] > mW) mW = blockRect[i][0]+blockRect[i][2]; if(blockRect[i][0]+blockRect[i][2] > mapWidth) mapWidth = blockRect[i][0]+blockRect[i][2];
if(blockRect[i][1]+blockRect[i][3] > mH) mH = blockRect[i][1]+blockRect[i][3]; if(blockRect[i][1]+blockRect[i][3] > mapHeight) mapHeight = blockRect[i][1]+blockRect[i][3];
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << mapWidth << " - " << mapHeight << std::endl;
//Very descriptive by itself //Very descriptive by itself
map.close(); 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){ int Maps::map(std::string mapName){
//Load map //Load map
loadMap(mapName + ".map"); loadMap(mapName + ".map");
int mapHeight = 12*sz; Texture level;
int mapWidth = 36*sz; 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 //Set the camera and pass the map and screen's dimensions
Camera camera(mapWidth,mapHeight,screenWidth,screenHeight); Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
@ -105,6 +106,8 @@ int Maps::map(std::string mapName){
SDL_SetRenderDrawColor(renderer,0,0,100,0xFF); SDL_SetRenderDrawColor(renderer,0,0,100,0xFF);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
level.render(&levelRect);
//Update the camera position //Update the camera position
camera.update(player.getRectangle().x,player.getRectangle().y); camera.update(player.getRectangle().x,player.getRectangle().y);

View File

@ -9,6 +9,7 @@
#include"camera.h" #include"camera.h"
#include<fstream> #include<fstream>
#include<algorithm> #include<algorithm>
#include"texture.h"
class Maps{ class Maps{
public: public:
@ -19,12 +20,12 @@ class Maps{
void loadMap(std::string path); void loadMap(std::string path);
SDL_Renderer* renderer; SDL_Renderer* renderer;
Player player;
int blockTotal; int blockTotal;
int** blockRect; int** blockRect;
int sz; int sz;
int screenWidth, screenHeight; int screenWidth, screenHeight;
int mapWidth, mapHeight;
int playerPosX, playerPosY; int playerPosX, playerPosY;
const Uint8* currentKeyStates; const Uint8* currentKeyStates;
}; };

View File

@ -29,6 +29,25 @@ int menu(SDL_Renderer* renderer){
button[i].h = btnHeight; 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; int select = 0;
bool wait = 0; bool wait = 0;
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
@ -66,6 +85,11 @@ int menu(SDL_Renderer* renderer){
SDL_RenderFillRect(renderer,&button[i]); SDL_RenderFillRect(renderer,&button[i]);
} }
play.render(&playRect);
exit.render(&exitRect);
options.render(&optionsRect);
if(currentKeyStates[SDL_SCANCODE_SPACE]){ if(currentKeyStates[SDL_SCANCODE_SPACE]){
if(select == 0) return 1; if(select == 0) return 1;
else if(select == 1) return 0; else if(select == 1) return 0;

View File

@ -10,6 +10,9 @@ Texture::Texture(){
//Initialize the SDL_Image library //Initialize the SDL_Image library
IMG_Init(IMG_INIT_PNG); 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){ void Texture::setRenderer(SDL_Renderer* render){
@ -20,6 +23,9 @@ void Texture::setRenderer(SDL_Renderer* render){
Texture::~Texture(){ Texture::~Texture(){
//Deallocate the texture when the class closes //Deallocate the texture when the class closes
free(); free();
IMG_Quit();
TTF_Quit();
}; };
void Texture::loadTexture(std::string path){ 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(){ void Texture::free(){
//Free texture if it exists //Free texture if it exists
if(texture != NULL){ if(texture != NULL){

View File

@ -5,6 +5,7 @@
#include<SDL.h> #include<SDL.h>
#include<SDL_image.h> #include<SDL_image.h>
#include<SDL_ttf.h>
#include<iostream> #include<iostream>
#include<string> #include<string>
@ -13,6 +14,7 @@ class Texture{
Texture(); Texture();
~Texture(); ~Texture();
void loadTexture(std::string path); void loadTexture(std::string path);
bool loadFromRendererText(std::string textureText, SDL_Color textColor);
void free(); void free();
void render(SDL_Rect* quad,SDL_Rect* frame); void render(SDL_Rect* quad,SDL_Rect* frame);
void render(SDL_Rect* quad); void render(SDL_Rect* quad);
@ -22,6 +24,7 @@ class Texture{
private: private:
SDL_Texture* texture; SDL_Texture* texture;
SDL_Renderer* renderer; SDL_Renderer* renderer;
TTF_Font *font = NULL;
int szW, szH; int szW, szH;
}; };