Bug fixed and font inclusion
This commit is contained in:
parent
786fb12847
commit
1da224483b
Binary file not shown.
Binary file not shown.
|
@ -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
|
||||
1 7 5 8 7 2 2 4 9 1 2 6 9 1 2 0 11 16 1 20 11 16 1
|
|
@ -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
|
Binary file not shown.
Binary file not shown.
|
@ -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();
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<char>(map), std::istreambuf_iterator<char>(), '\n');
|
||||
map >> blockTotal;
|
||||
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
|
||||
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);
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include"camera.h"
|
||||
#include<fstream>
|
||||
#include<algorithm>
|
||||
#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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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){
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include<SDL.h>
|
||||
#include<SDL_image.h>
|
||||
#include<SDL_ttf.h>
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue