1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <string>
int width = 1100,
height = 700,
fps = 25,
moveSpeed = 7;
bool quit = false;
const Uint8 *state = SDL_GetKeyboardState(NULL);
SDL_Surface *man = NULL;
SDL_Surface *wall1 = NULL;
SDL_Surface *wall2 = NULL;
SDL_Texture *texMan = NULL;
SDL_Texture *texWall1 = NULL;
SDL_Texture *texWall2 = NULL;
SDL_Rect rectWall1;
SDL_Rect rectWall2;
SDL_Event event;
SDL_Renderer *renderer;
void recreateBackground();
int moveObj(std::string direction, SDL_Rect obj);
int main ( int argc, char** argv )
{
if(SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
if(SDL_Init(SDL_INIT_EVENTS) < 0 ) return 2;
if(SDL_Init(SDL_INIT_TIMER) < 0 ) return 3;
SDL_Window* win = SDL_CreateWindow("Collision Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_SHOWN);
if(win == NULL) return 4;
renderer = SDL_CreateRenderer(win, -1, 0);
//---------------------------------------------------------------------------------------------------
man = IMG_Load("gfx\\man.png");
if(man == NULL) return 4;
texMan = SDL_CreateTextureFromSurface(renderer, man);
SDL_FreeSurface(man);
SDL_Rect rectMan;
rectMan.x = 0;
rectMan.y = 0;
rectMan.w = 20;
rectMan.h = 20;
wall1 = IMG_Load("gfx\\wall1.png");
if(wall1 == NULL) return 5;
texWall1 = SDL_CreateTextureFromSurface(renderer, wall1);
SDL_FreeSurface(wall1);
rectWall1.x = 300;
rectWall1.y = 300;
rectWall1.w = 10;
rectWall1.h = 100;
wall2 = IMG_Load("gfx\\wall2.png");
if(wall2 == NULL) return 6;
texWall2 = SDL_CreateTextureFromSurface(renderer, wall2);
SDL_FreeSurface(wall2);
rectWall2.x = 700;
rectWall2.y = 280;
rectWall2.w = 100;
rectWall2.h = 10;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texWall1, NULL, &rectWall1);
SDL_RenderCopy(renderer, texWall2, NULL, &rectWall2);
SDL_RenderCopy(renderer, texMan, NULL, &rectMan);
SDL_RenderPresent(renderer);
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT) quit = true;
SDL_PumpEvents();
if(state[SDL_SCANCODE_ESCAPE]) quit = true;
if(state[SDL_SCANCODE_DOWN]) rectMan.y = moveObj("Down", rectMan);
if(state[SDL_SCANCODE_LEFT]) rectMan.x = moveObj("Left", rectMan);
if(state[SDL_SCANCODE_RIGHT]) rectMan.x = moveObj("Right", rectMan);
if(state[SDL_SCANCODE_UP]) rectMan.y = moveObj("Up", rectMan);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
int moveObj(std::string direction, SDL_Rect obj)
{
if(direction == "Left") obj.x -= moveSpeed;
if(direction == "Right") obj.x += moveSpeed;
if(direction == "Down") obj.y += moveSpeed;
if(direction == "Up") obj.y -= moveSpeed;
recreateBackground();
SDL_RenderCopy(renderer, texMan, NULL, &obj);
SDL_RenderPresent(renderer);
if(direction == "Left"||direction == "Right") return obj.x;
if(direction == "Down"||direction == "Up") return obj.y;
}
void recreateBackground()
{
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texWall1, NULL, &rectWall1);
SDL_RenderCopy(renderer, texWall2, NULL, &rectWall2);
}
|