Draw bricks
This commit is contained in:
parent
efcf9cfffe
commit
e6c1e2a15a
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ CC=clang++
|
|||
all: build run
|
||||
|
||||
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:
|
||||
./breakout
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
#include <iostream>
|
||||
#include "SDL2/SDL.h"
|
||||
#include "breakout.h"
|
||||
#include "scene.h"
|
||||
#include "breakout.hpp"
|
||||
#include "scene.hpp"
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
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;
|
||||
}
|
||||
|
||||
DEBUG("[?] Creating window");
|
||||
SDL_Window *window = SDL_CreateWindow(
|
||||
"Breakout",
|
||||
SCREEN_X, SCREEN_Y,
|
||||
|
@ -20,7 +21,9 @@ int main(int argc, char const *argv[]) {
|
|||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
DEBUG("[!] Window created");
|
||||
|
||||
DEBUG("[?] Creating renderer");
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(
|
||||
window, // Window
|
||||
-1, // Video driver. -1 means "any compatible"
|
||||
|
@ -32,15 +35,14 @@ int main(int argc, char const *argv[]) {
|
|||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
DEBUG("[!] Renderer created");
|
||||
|
||||
Scene scene(window, renderer);
|
||||
Scene scene(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;
|
||||
}
|
||||
|
@ -62,11 +64,8 @@ int main(int argc, char const *argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
if (has_event) {
|
||||
scene.render();
|
||||
} else {
|
||||
SDL_Delay(1);
|
||||
}
|
||||
SDL_Delay(10);
|
||||
}
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
|
|
|
@ -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
|
|
@ -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)
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
};
|
|
@ -1,25 +1,33 @@
|
|||
#include <iostream>
|
||||
#include "SDL2/SDL.h"
|
||||
#include "breakout.h"
|
||||
#include "scene.h"
|
||||
#include "scene.hpp"
|
||||
|
||||
Scene::Scene(SDL_Window *w, SDL_Renderer *r) {
|
||||
window = w;
|
||||
Scene::Scene(SDL_Renderer *r) {
|
||||
DEBUG("[?] Initializing scene");
|
||||
renderer = r;
|
||||
|
||||
pad.x = (SCREEN_WIDTH - PAD_WIDTH) / 2;
|
||||
pad.y = (SCREEN_HEIGHT - PAD_HEIGHT);
|
||||
pad.w = PAD_WIDTH;
|
||||
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() {}
|
||||
|
||||
void Scene::render() {
|
||||
SDL_SetRenderDrawColor(renderer, BG_COLOR);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, PAD_COLOR);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderFillRect(renderer, &pad);
|
||||
for (int i = 0; i < BRICK_ROWS * BRICK_COLS; i++) {
|
||||
bricks[i].render();
|
||||
}
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
|
|
21
src/scene.h
21
src/scene.h
|
@ -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];
|
||||
};
|
|
@ -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;
|
||||
};
|
Loading…
Reference in New Issue