C::B , menu and better organization

Project adapted to Code::Blocks and Windows, new main menu and a better organization, putting all the source files in a directory and the entity source files in other one inside the source directory. New icon for the final executable.
This commit is contained in:
Pòsweg 2017-06-16 17:24:22 +02:00
parent c03f5db6dd
commit e94936ec99
24 changed files with 178 additions and 53 deletions

14
.gitignore vendored
View File

@ -59,5 +59,15 @@ reated by https://www.gitignore.io/api/vim,c++,linux
Session.vim
# temporary
.netrwhist
# auto-generated tag files
tags
### CodeBlocks ###
# specific to CodeBlocks IDE
*.cbp
*.layout
*.depend
# generated directories
bin/
obj/
# auto-generated tag files

BIN
main

Binary file not shown.

View File

@ -3,7 +3,7 @@
#ifndef __CAMERA_H_INCLUDED__
#define __CAMERA_H_INCLUDED__
#include"player.h"
#include"entity/player.h"
class Camera{
public:

View File

@ -7,7 +7,7 @@ void Core::init(){
gWindow = SDL_CreateWindow("Platform Test!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);
sz = SCREEN_WIDTH/16;
}
@ -23,7 +23,7 @@ void Core::close(){
int Core::coreInit(){
init();
bool quit = false;
/*bool quit = false;
SDL_Event e;
while(!quit){
@ -37,15 +37,18 @@ int Core::coreInit(){
SDL_RenderPresent(gRenderer);
}*/
if(menu(gRenderer) == 1){
map1();
}
map1();
close();
return 0;
};
void Core::map1(){
Player posweg(&gRenderer);
Player posweg(&gRenderer);
bool quit = false;
SDL_Event e;
@ -61,8 +64,8 @@ void Core::map1(){
Camera camera(36*sz,SCREEN_WIDTH);
posweg.print(100,100,camera.getPosX());
posweg.print(40,8*sz ,camera.getPosX());
camera.update(posweg.getRectangle().x,posweg.getRectangle().y);
Block wallA(8*sz,7*sz,sz*2,sz*2,&gRenderer,&posweg,camera.getPosX());
Block wallB(4*sz,9*sz,sz,sz*2,&gRenderer,&posweg,camera.getPosX());
@ -74,5 +77,5 @@ void Core::map1(){
SDL_RenderPresent(gRenderer);
}
};

View File

@ -4,12 +4,13 @@
#define __CORE_H_INCLUDED__
#include<iostream>
#include<SDL2/SDL.h>
#include"entity.h"
#include"player.h"
#include"block.h"
#include"powerup.h"
#include<SDL.h>
#include"entity/entity.h"
#include"entity/player.h"
#include"entity/block.h"
#include"entity/powerup.h"
#include"camera.h"
#include"menu.h"
class Core{
public:
@ -17,7 +18,7 @@ class Core{
private:
void init();
void close();
void map1();
const int SCREEN_WIDTH = 640;
@ -26,7 +27,7 @@ class Core{
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//Player posweg(&gRenderer);
};

View File

@ -1,9 +1,9 @@
//Delta time class header
//Delta time class header<
#ifndef __DT_H_INCLUDED__
#define __DT_H_INCLUDED__
#include<SDL2/SDL.h>
#include<SDL.h>
class DeltaTime{
public:

View File

@ -4,7 +4,7 @@
#define __BLOCK_H_INCLUDED__
#include"entity.h"
#include<SDL2/SDL.h>
#include<SDL.h>
#include<iostream>
#include"player.h"

View File

@ -3,7 +3,7 @@
#ifndef __ENTITY_H_INCLUDED__
#define __ENTITY_H_INCLUDED__
#include<SDL2/SDL.h>
#include<SDL.h>
#include<iostream>
class Entity{

View File

@ -2,7 +2,7 @@
#include"player.h"
void Player::print(int x,int y,int cameraX){
void Player::print(int x,int y,int cameraX){
if(first == true){
first = false;
posX = x;
@ -10,7 +10,7 @@ void Player::print(int x,int y,int cameraX){
}
oldPosX = posX;
oldPosY = posY;
oldPosY = posY;
rect.x = posX;
rect.y = posY;
@ -41,6 +41,8 @@ Player::Player(SDL_Renderer** render){
ply.setRenderer(renderer);
loadMedia();
//first = true;
ground = false;
};
void Player::loadMedia(){
@ -69,10 +71,10 @@ int Player::intVelX(){
void Player::move(){
//float dt = dTime.getDt();
float dt = 0.016;
//Set keyboard variable
//Set keyboard variable
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
//Intializing variables
int direction = 0; //1 = right, 0 = idle/both, -1 = left
int dur = 4; //Divide transition time
@ -115,7 +117,7 @@ void Player::move(){
if(intVelX() > 0) velocityX = 0;
}
else if(velocityX <= 1 or velocityX >= -1) velocityX = 0;
//Jump and gravity logic
if(ground){
isRunning = false;
@ -129,14 +131,14 @@ void Player::move(){
else{
if(isRunning == false) run = 1;
}
if(run!=1) ifRunning = true;
//Get the position and update the velY with gravity
velocityY -= gravity * dt;
float x = velocityX * dt * run;
float y = velocityY * dt;
//Convert and set new int position
posX += static_cast<int>(x);
posY -= static_cast<int>(y+0.5);
@ -154,7 +156,7 @@ int Player::check(SDL_Rect rectA, int type){
//Float-ize the position
float movX = posX, movY = posY;
//Move the player out of the rectangle
if(type == 1){
while(((movX >= bX and movX < b2X)
@ -167,7 +169,7 @@ int Player::check(SDL_Rect rectA, int type){
movY -= (posY - oldPosY)/2;
collision = 1;
}
//Correct possible position issues
if(collision == 1){
//Vertical adjustement
@ -200,7 +202,7 @@ int Player::check(SDL_Rect rectA, int type){
//Set and int-ize the position*/
posX = static_cast<int>(movX);
posY = static_cast<int>(movY);
//Check collsion type and reset velocities
//Vertical collisions
if((posX >= bX and posX < b2X)

View File

@ -1,14 +1,14 @@
//Player class header
//Player class header<
#ifndef __PLAYER_H_INCLUDED__
#define __PLAYER_H_INCLUDED__
#include<SDL2/SDL.h>
#include<SDL.h>
#include<iostream>
#include<cmath>
#include"dt.h"
#include"../dt.h"
#include"entity.h"
#include"texture.h"
#include"../texture.h"
class Entity;
@ -34,7 +34,7 @@ class Player: public Entity{
bool first;
int power = 0;
PosuTexture ply;
Texture ply;
SDL_Rect plyFrame[3];
SDL_Rect plyRun;
};

View File

@ -3,7 +3,7 @@
#ifndef __POWERUP_H_INCLUDED__
#define __POWERUP_H_INCLUDED__
#include<SDL2/SDL.h>
#include<SDL.h>
#include<iostream>
#include"entity.h"
#include"player.h"

BIN
source/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

6
source/icon.rc Normal file
View File

@ -0,0 +1,6 @@
#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED
1 ICON "icon.ico"
#endif // RESOURCE_RC_INCLUDED

86
source/menu.cpp Normal file
View File

@ -0,0 +1,86 @@
#include "menu.h"
int menu(SDL_Renderer* renderer){
int SCREEN_WIDTH, SCREEN_HEIGHT;
SDL_GetRendererOutputSize(renderer,&SCREEN_WIDTH,&SCREEN_HEIGHT);
bool quit = false;
SDL_Event e;
Texture txLogo;
txLogo.setRenderer(&renderer);
txLogo.loadTexture("textures/title.png");
int logoVmargin = 40;
SDL_Rect logo = {(SCREEN_WIDTH/2)-txLogo.getWidth()/2,logoVmargin,txLogo.getWidth(),txLogo.getHeight()};
int btnSpc = SCREEN_HEIGHT - (logo.y + logo.h);
int buttonNum = 3;
SDL_Rect button[buttonNum];
int btnWidth = SCREEN_WIDTH/3;
int btnHeight = btnSpc/buttonNum/2;
int spc = (btnSpc-buttonNum*btnHeight)/(buttonNum+1);
for(int i = 0; i < buttonNum; i++){
button[i].x = SCREEN_WIDTH/2-btnWidth/2;
button[i].y = ((i+1)*spc+i*btnHeight)+SCREEN_HEIGHT-btnSpc;
button[i].w = btnWidth;
button[i].h = btnHeight;
std::cout << "Button " << i+1
<< " set as -> x: " << button[i].x
<< " y: " << button[i].y
<< " w: " << button[i].w
<< " h: " << button[i].h
<< std::endl;
}
int select = 0;
bool wait = 0;
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
std::cout << std::endl << "Entering main loop..." << std::endl;
while(!quit){
while(SDL_PollEvent(&e)!=0){
if(e.type == SDL_QUIT){
quit = true;
}
}
SDL_SetRenderDrawColor(renderer,100,200,200,0xFF);
SDL_RenderClear(renderer);
int dir = 0;
if(currentKeyStates[SDL_SCANCODE_UP]) dir--;
if(currentKeyStates[SDL_SCANCODE_DOWN])dir++;
if(dir != 0){
if(!wait) select += dir;
wait = true;
}
else wait = false;
if(select >= buttonNum) select = 0;
if(select < 0) select = buttonNum-1;
txLogo.render(&logo);
for(int i = 0; i < buttonNum; i++){
if(static_cast<int>(select) == i){
SDL_SetRenderDrawColor(renderer,80,80,80,0xFF);
if(currentKeyStates[SDL_SCANCODE_SPACE]) SDL_SetRenderDrawColor(renderer,200,200,200,0xFF);
}
else SDL_SetRenderDrawColor(renderer,128,128,128,0xFF);
SDL_RenderFillRect(renderer,&button[i]);
}
if(currentKeyStates[SDL_SCANCODE_SPACE]){
if(select == 0) return 1;
else if(select == 2) break;
}
SDL_RenderPresent(renderer);
}
return 0;
}

10
source/menu.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef MENU_H
#define MENU_H
#include<SDL.h>
#include<iostream>
#include"texture.h"
int menu(SDL_Renderer* render);
#endif // MENU_H

View File

@ -1,6 +1,6 @@
#include"texture.h"
PosuTexture::PosuTexture(){
Texture::Texture(){
//Initialize variables
szW = 0;
szH = 0;
@ -9,19 +9,19 @@ PosuTexture::PosuTexture(){
//Set renderer
};
void PosuTexture::setRenderer(SDL_Renderer** render){
void Texture::setRenderer(SDL_Renderer** render){
renderer = render;
};
PosuTexture::~PosuTexture(){
Texture::~Texture(){
//Deallocate
//free();
};
void PosuTexture::loadTexture(std::string path){
void Texture::loadTexture(std::string path){
//Get rid of preexisting texture
free();
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
@ -47,7 +47,7 @@ void PosuTexture::loadTexture(std::string path){
}
};
void PosuTexture::free(){
void Texture::free(){
//Free texture if it exists
if(texture != NULL){
SDL_DestroyTexture(texture);
@ -57,8 +57,8 @@ void PosuTexture::free(){
}
};
void PosuTexture::render(SDL_Rect* quad,SDL_Rect* frame){
//void PosuTexture::render(SDL_Rect* quad){
void Texture::render(SDL_Rect* quad,SDL_Rect* frame){
//void Texture::render(SDL_Rect* quad){
//Set clip rendering dimensions
/*if(frame != NULL){
quad.w = frame->w;
@ -71,10 +71,16 @@ void PosuTexture::render(SDL_Rect* quad,SDL_Rect* frame){
//std::cout << "ye" << std::endl;
};
int PosuTexture::getWidth(){
void Texture::render(SDL_Rect* quad){
SDL_Rect frame = {0,0,quad->w,quad->h};
SDL_RenderCopy(*renderer,texture,&frame,quad);
};
int Texture::getWidth(){
return szW;
};
int PosuTexture::getHeight(){
int Texture::getHeight(){
return szH;
};

View File

@ -3,18 +3,19 @@
#ifndef __TEXTURE_H_INCLUDED__
#define __TEXTURE_H_INCLUDED__
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#include<SDL.h>
#include<SDL_image.h>
#include<iostream>
#include<string>
class PosuTexture{
class Texture{
public:
PosuTexture();
~PosuTexture();
Texture();
~Texture();
void loadTexture(std::string path);
void free();
void render(SDL_Rect* quad,SDL_Rect* frame);
void render(SDL_Rect* quad);
//void render(SDL_Rect* quad);
int getWidth();
int getHeight();

BIN
textures/title.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB