Implement a basic scene class
This commit is contained in:
parent
bb07d62bc4
commit
efcf9cfffe
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ CC=clang++
|
|||
all: build run
|
||||
|
||||
build:
|
||||
$(CC) -lSDL2 main.cpp -o breakout
|
||||
$(CC) -lSDL2 src/scene.cpp src/breakout.cpp -o breakout
|
||||
|
||||
run:
|
||||
./breakout
|
||||
|
|
|
@ -1,17 +1,7 @@
|
|||
#include <iostream>
|
||||
#include "SDL2/SDL.h"
|
||||
|
||||
#define SCREEN_X 100
|
||||
#define SCREEN_Y 100
|
||||
#define SCREEN_WIDTH 640
|
||||
#define SCREEN_HEIGHT 480
|
||||
#define PAD_WIDTH 200
|
||||
#define PAD_HEIGHT 20
|
||||
|
||||
#define KEY_MOVE_STEP 5
|
||||
|
||||
#define BG_COLOR 0, 0, 0, 255
|
||||
#define PAD_COLOR 0, 0, 255, 255
|
||||
#include "breakout.h"
|
||||
#include "scene.h"
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
|
@ -43,31 +33,14 @@ int main(int argc, char const *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Setting background color to black
|
||||
SDL_SetRenderDrawColor(renderer, BG_COLOR);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high.
|
||||
SDL_Rect pad = {
|
||||
.x = (SCREEN_WIDTH - PAD_WIDTH) / 2,
|
||||
.y = (SCREEN_HEIGHT - PAD_HEIGHT),
|
||||
.w = PAD_WIDTH,
|
||||
.h = PAD_HEIGHT
|
||||
};
|
||||
|
||||
// Set render color to blue ( rect will be rendered in this color )
|
||||
SDL_SetRenderDrawColor(renderer, PAD_COLOR);
|
||||
|
||||
// Render rect
|
||||
SDL_RenderFillRect(renderer, &pad);
|
||||
|
||||
// Render the rect to the screen
|
||||
SDL_RenderPresent(renderer);
|
||||
Scene scene(window, renderer);
|
||||
|
||||
SDL_Event e;
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
bool has_event = false;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
has_event = true;
|
||||
if (e.type == SDL_QUIT) {
|
||||
quit = true;
|
||||
}
|
||||
|
@ -77,32 +50,24 @@ int main(int argc, char const *argv[]) {
|
|||
quit = true;
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
pad.x -= KEY_MOVE_STEP;
|
||||
scene.move_pad_relative(-KEY_MOVE_STEP);
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
pad.x += KEY_MOVE_STEP;
|
||||
scene.move_pad_relative(KEY_MOVE_STEP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (e.type == SDL_MOUSEMOTION) {
|
||||
pad.x = e.motion.x - PAD_WIDTH / 2;
|
||||
}
|
||||
|
||||
if (pad.x < 0) {
|
||||
pad.x = 0;
|
||||
} else if (pad.x > SCREEN_WIDTH - PAD_WIDTH) {
|
||||
pad.x = SCREEN_WIDTH - PAD_WIDTH;
|
||||
scene.move_pad(e.motion.x - PAD_WIDTH / 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Re-rendering
|
||||
SDL_SetRenderDrawColor(renderer, BG_COLOR);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, PAD_COLOR);
|
||||
SDL_RenderFillRect(renderer, &pad);
|
||||
SDL_RenderPresent(renderer);
|
||||
if (has_event) {
|
||||
scene.render();
|
||||
} else {
|
||||
SDL_Delay(1);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
|
@ -0,0 +1,6 @@
|
|||
#define SCREEN_X 300
|
||||
#define SCREEN_Y 100
|
||||
#define SCREEN_WIDTH 400
|
||||
#define SCREEN_HEIGHT 600
|
||||
|
||||
#define KEY_MOVE_STEP 5
|
|
@ -0,0 +1,38 @@
|
|||
#include <iostream>
|
||||
#include "SDL2/SDL.h"
|
||||
#include "breakout.h"
|
||||
#include "scene.h"
|
||||
|
||||
Scene::Scene(SDL_Window *w, SDL_Renderer *r) {
|
||||
window = w;
|
||||
renderer = r;
|
||||
|
||||
pad.x = (SCREEN_WIDTH - PAD_WIDTH) / 2;
|
||||
pad.y = (SCREEN_HEIGHT - PAD_HEIGHT);
|
||||
pad.w = PAD_WIDTH;
|
||||
pad.h = PAD_HEIGHT;
|
||||
}
|
||||
|
||||
Scene::~Scene() {}
|
||||
|
||||
void Scene::render() {
|
||||
SDL_SetRenderDrawColor(renderer, BG_COLOR);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, PAD_COLOR);
|
||||
SDL_RenderFillRect(renderer, &pad);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
void Scene::move_pad(int x) {
|
||||
pad.x = x;
|
||||
|
||||
if (pad.x < 0) {
|
||||
pad.x = 0;
|
||||
} else if (pad.x > SCREEN_WIDTH - PAD_WIDTH) {
|
||||
pad.x = SCREEN_WIDTH - PAD_WIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::move_pad_relative(int delta) {
|
||||
move_pad(pad.x + delta);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#define PAD_WIDTH 200
|
||||
#define PAD_HEIGHT 20
|
||||
|
||||
#define BG_COLOR 0, 0, 0, 255
|
||||
#define PAD_COLOR 0, 0, 255, 255
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene(SDL_Window *w, SDL_Renderer *r);
|
||||
~Scene();
|
||||
|
||||
void render();
|
||||
void move_pad(int x);
|
||||
void move_pad_relative(int delta);
|
||||
private:
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
|
||||
SDL_Rect pad;
|
||||
SDL_Rect blocks[10];
|
||||
};
|
Loading…
Reference in New Issue