1
0
Fork 0

Draw bricks

This commit is contained in:
Gregory Eremin 2015-10-04 15:48:57 +03:00
parent efcf9cfffe
commit e6c1e2a15a
9 changed files with 127 additions and 47 deletions

View File

@ -3,7 +3,7 @@ CC=clang++
all: build run all: build run
build: build:
$(CC) -lSDL2 src/scene.cpp src/breakout.cpp -o breakout $(CC) -lSDL2 src/brick.cpp src/scene.cpp src/breakout.cpp -o breakout
run: run:
./breakout ./breakout

View File

@ -1,14 +1,15 @@
#include <iostream> #include <iostream>
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include "breakout.h" #include "breakout.hpp"
#include "scene.h" #include "scene.hpp"
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) { if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; std::cout << "SDL_Init Error: " << std::endl;
return 1; return 1;
} }
DEBUG("[?] Creating window");
SDL_Window *window = SDL_CreateWindow( SDL_Window *window = SDL_CreateWindow(
"Breakout", "Breakout",
SCREEN_X, SCREEN_Y, SCREEN_X, SCREEN_Y,
@ -20,7 +21,9 @@ int main(int argc, char const *argv[]) {
SDL_Quit(); SDL_Quit();
return 1; return 1;
} }
DEBUG("[!] Window created");
DEBUG("[?] Creating renderer");
SDL_Renderer *renderer = SDL_CreateRenderer( SDL_Renderer *renderer = SDL_CreateRenderer(
window, // Window window, // Window
-1, // Video driver. -1 means "any compatible" -1, // Video driver. -1 means "any compatible"
@ -32,15 +35,14 @@ int main(int argc, char const *argv[]) {
SDL_Quit(); SDL_Quit();
return 1; return 1;
} }
DEBUG("[!] Renderer created");
Scene scene(window, renderer); Scene scene(renderer);
SDL_Event e; SDL_Event e;
bool quit = false; bool quit = false;
while (!quit) { while (!quit) {
bool has_event = false;
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
has_event = true;
if (e.type == SDL_QUIT) { if (e.type == SDL_QUIT) {
quit = true; quit = true;
} }
@ -62,11 +64,8 @@ int main(int argc, char const *argv[]) {
} }
} }
if (has_event) {
scene.render(); scene.render();
} else { SDL_Delay(10);
SDL_Delay(1);
}
} }
SDL_DestroyWindow(window); SDL_DestroyWindow(window);

View File

@ -1,6 +0,0 @@
#define SCREEN_X 300
#define SCREEN_Y 100
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 600
#define KEY_MOVE_STEP 5

17
src/breakout.hpp Normal file
View File

@ -0,0 +1,17 @@
#import <iostream>
#define SCREEN_X 300
#define SCREEN_Y 100
#define SCREEN_WIDTH 407
#define SCREEN_HEIGHT 600
#define PAD_WIDTH 100
#define PAD_HEIGHT 10
#define KEY_MOVE_STEP 5
#define BRICK_ROWS 7
#define BRICK_COLS 8
#define BRICK_WIDTH 50
#define BRICK_HEIGHT 15
#define DEBUG(str) (std::cerr << str << std::endl)

39
src/brick.cpp Normal file
View File

@ -0,0 +1,39 @@
#import "brick.hpp"
Brick::Brick(SDL_Renderer *r, int x, int y, Color c) {
renderer = r;
brick.x = x;
brick.y = y;
brick.w = BRICK_WIDTH;
brick.h = BRICK_HEIGHT;
color = c;
}
Brick::~Brick() {}
void Brick::render() {
switch (color) {
case RED:
SDL_SetRenderDrawColor(renderer, 255, 51, 51, 255);
break;
case ORANGE:
SDL_SetRenderDrawColor(renderer, 255, 153, 51, 255);
break;
case YELLOW:
SDL_SetRenderDrawColor(renderer, 255, 255, 51, 255);
break;
case GREEN:
SDL_SetRenderDrawColor(renderer, 153, 255, 51, 255);
break;
case LIGHTBLUE:
SDL_SetRenderDrawColor(renderer, 51, 255, 255, 255);
break;
case BLUE:
SDL_SetRenderDrawColor(renderer, 51, 153, 255, 255);
break;
case PURPLE:
SDL_SetRenderDrawColor(renderer, 153, 51, 255, 255);
break;
}
SDL_RenderFillRect(renderer, &brick);
}

25
src/brick.hpp Normal file
View File

@ -0,0 +1,25 @@
#include "SDL2/SDL.h"
#include "breakout.hpp"
typedef enum Color {
RED,
ORANGE,
YELLOW,
GREEN,
LIGHTBLUE,
BLUE,
PURPLE
} Color;
class Brick {
public:
Brick(SDL_Renderer *r, int x, int y, Color c);
~Brick();
void render();
private:
SDL_Renderer *renderer;
SDL_Rect brick;
Color color;
};

View File

@ -1,25 +1,33 @@
#include <iostream> #include "scene.hpp"
#include "SDL2/SDL.h"
#include "breakout.h"
#include "scene.h"
Scene::Scene(SDL_Window *w, SDL_Renderer *r) { Scene::Scene(SDL_Renderer *r) {
window = w; DEBUG("[?] Initializing scene");
renderer = r; renderer = r;
pad.x = (SCREEN_WIDTH - PAD_WIDTH) / 2; pad.x = (SCREEN_WIDTH - PAD_WIDTH) / 2;
pad.y = (SCREEN_HEIGHT - PAD_HEIGHT); pad.y = (SCREEN_HEIGHT - PAD_HEIGHT);
pad.w = PAD_WIDTH; pad.w = PAD_WIDTH;
pad.h = PAD_HEIGHT; pad.h = PAD_HEIGHT;
for (int i = 0; i < BRICK_ROWS; i++) {
for (int j = 0; j < BRICK_COLS; j++) {
bricks.push_back(Brick(renderer, j * BRICK_WIDTH + j, i * BRICK_HEIGHT + i, (Color)i));
}
}
DEBUG("[!] Scene initialized");
} }
Scene::~Scene() {} Scene::~Scene() {}
void Scene::render() { void Scene::render() {
SDL_SetRenderDrawColor(renderer, BG_COLOR); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, PAD_COLOR); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &pad); SDL_RenderFillRect(renderer, &pad);
for (int i = 0; i < BRICK_ROWS * BRICK_COLS; i++) {
bricks[i].render();
}
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }

View File

@ -1,21 +0,0 @@
#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];
};

19
src/scene.hpp Normal file
View File

@ -0,0 +1,19 @@
#include <vector>
#include "SDL2/SDL.h"
#include "breakout.hpp"
#import "brick.hpp"
class Scene {
public:
Scene(SDL_Renderer *r);
~Scene();
void render();
void move_pad(int x);
void move_pad_relative(int delta);
private:
SDL_Renderer *renderer;
SDL_Rect pad;
std::vector<Brick> bricks;
};