platform-test/main.cpp

123 lines
2.4 KiB
C++

//main file
#include<iostream>
#include<SDL2/SDL.h>
#include"player.h"
#include"block.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//12 sz of height and 16 of width
const int sz = SCREEN_WIDTH/16;
void init();
bool loadMedia();
void close();
void gameLoop();
int check(SDL_Rect rectA, SDL_Rect rectB);
Player posweg;
Block ground;
bool loadMedia(){
bool success = true;
return success;
}
void init(){
SDL_Init(SDL_INIT_VIDEO);
//IMG_Init(IMG_INIT_JPG);
gWindow = SDL_CreateWindow("Plataform 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);
}
void close(){
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
//IMG_Quit();
SDL_Quit();
}
void gameLoop(){
SDL_SetRenderDrawColor(gRenderer,0,0,0,0xFF);
SDL_RenderClear(gRenderer);
Block wallA;
Block wallB;
Block wallC;
wallA.print(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,gRenderer);
wallB.print(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,gRenderer);
wallC.print(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,gRenderer);
ground.print(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,gRenderer);
posweg.print();
posweg.check(wallA.getRectangle());
posweg.check(wallB.getRectangle());
posweg.check(wallC.getRectangle());
posweg.check(ground.getRectangle());
SDL_RenderPresent(gRenderer);
}
int main(int argc, char* args[]){
init();
loadMedia();
bool quit = false;
SDL_Event e;
posweg.init(100,100,sz,sz,&gRenderer);
while(!quit){
while(SDL_PollEvent(&e)!=0){
if(e.type == SDL_QUIT){
quit = true;
}
}
gameLoop();
}
close();
return 0;
}
int check(SDL_Rect rectA, SDL_Rect rectB){
int collision = 0;
int aX = rectA.x;
int aY = rectA.y;
int a2X = rectA.x + rectA.w;
int a2Y = rectA.y + rectA.h;
int bX = rectB.x;
int bY = rectB.y;
int b2X = rectB.x + rectB.w;
int b2Y = rectB.y + rectB.h;
if(aX > bX and aX < b2X){
if(aY > bY and aY < b2Y) collision += 1;
if(a2Y > bY and a2Y < b2Y) collision += 2;
}
if(a2X > bX and a2X < b2X){
if(aY > bY and aY < b2Y) collision += 4;
if(a2Y > bY and a2Y < b2Y) collision += 8;
}
//std::cout<<aX<<" "<<a2X<<" "<<aY<<" "<<a2Y<<" / ";
//std::cout<<bX<<" "<<b2X<<" "<<bY<<" "<<b2Y;
return collision;
};