Browse Source

boilerplate

master
alistair 4 years ago
commit
5cab363421
  1. 5
      Makefile
  2. 109
      game.cpp
  3. 78
      particles.cpp
  4. 64
      particles.h

5
Makefile

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
all:
g++ -c particles.cpp -lSDL2 -lSDL2_image -lSDL2_ttf -g -o particles.o
g++ -c game.cpp -lSDL2 -lSDL2_image -lSDL2_ttf -g -o game.o
g++ -lSDL2 -lSDL2_image -lSDL2_ttf -g particles.o game.o -o main

109
game.cpp

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <string>
#include <deque>
#include <iostream>
#include <vector>
#include "particles.h"
namespace pengine {
class GameWindow {
private:
struct SDL_Window *window;
std::string name;
public:
struct SDL_Renderer *renderer;
GameWindow(int width, int height, std::string name) {
SDL_Init(SDL_INIT_EVERYTHING);
this->name = name;
window = SDL_CreateWindow(name.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width, height,
SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC);
}
~GameWindow() {
SDL_DestroyWindow(window);
SDL_Quit();
}
};
class Game {
GameWindow win;
Grid grid;
SDL_Texture *grid_texture;
SDL_Renderer *ren;
public:
Game(int x, int y, int gx, int gy):
win{GameWindow {x,y,"game"}},
grid{Grid{gx, gy}}
{
ren = win.renderer;
grid_texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, gx,gy);
}
~Game() {
SDL_DestroyTexture(grid_texture);
}
void update() {
grid.update();
}
void draw() {
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
grid.update_texture(grid_texture);
SDL_RenderCopy(ren, grid_texture, NULL, NULL);
SDL_RenderPresent(ren);
SDL_RenderClear(ren);
}
void run() {
while (true) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return;
case SDL_KEYDOWN:
case SDL_KEYUP:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
continue;
}
}
update();
draw();
}
}
};
}
int main(int argc, char **argv) {
const char *error = SDL_GetError();
if (error) {
std::cout << error;
}
pengine::Game game {pengine::Game(500,500, 100,200)};
game.run();
return 0;
}

78
particles.cpp

@ -0,0 +1,78 @@ @@ -0,0 +1,78 @@
#include "particles.h"
#include <iostream>
namespace pengine {
PixelColour::PixelColour(short r, short b, short g, short a) {
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
PixelColour::PixelColour(short r, short b, short g) {
this->r = r;
this->g = g;
this->b = b;
this->a = 255;
}
PixelColour::PixelColour(SDL_Color c) {
this->r = c.r;
this->g = c.g;
this->b = c.b;
this->a = c.a;
}
SDL_Color PixelColour::get_sdlcol() {
SDL_Color c;
c.r = r;
c.b = b;
c.g = g;
c.a = a;
return c;
}
Uint32 PixelColour::get_pixelcol() {
return pixel;
}
Grid::Grid(int xs, int ys) {
xsize = xs;
ysize = ys;
grid = new Particle *[xs * ys] {};
pixels = new Uint32[xs * ys] {};
}
Grid::~Grid() {
delete grid;
delete pixels;
}
void Grid::update() {
for (int i = 0; i < xsize * ysize; i++) {
if (grid[i]) {
grid[i]->update();
}
}
}
void Grid::update_texture(SDL_Texture *texture) {
PixelColour defaultcol = PixelColour(255,0,255,255);
for (int i = 0; i < xsize; i++) {
for (int j = 0; j < ysize; j++) {
int p = j * xsize + i;
if (grid[p]) {
// pixels[i] = grid[i]->get_colour().get_pixelcol();
} else {
pixels[p] = defaultcol.get_pixelcol();
}
}
}
// SDL_Rect r {.x = 0, .y = 0, .w = xsize, .h = ysize};
SDL_UpdateTexture(texture, NULL, pixels, xsize * sizeof(Uint32));
}
}

64
particles.h

@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
#ifndef PARTICLES_H
#define PARTICLES_H
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
namespace pengine {
enum class ParticleID
{
SAND,
};
class PixelColour {
union {
struct {
Uint8 b;
Uint8 g;
Uint8 r;
Uint8 a;
};
Uint32 pixel;
};
public:
Uint32 get_pixelcol();
SDL_Color get_sdlcol();
PixelColour(short r, short b, short g, short a);
PixelColour(short r, short b, short g);
PixelColour(SDL_Color c);
};
class Particle
{
public:
enum ParticleID id;
virtual PixelColour get_colour() = 0;
virtual void update(void) = 0;
};
class Grid {
private:
Particle **grid;
Uint32 *pixels;
int xsize;
int ysize;
public:
Grid(int xs, int ys);
virtual ~Grid();
void update();
void update_texture(SDL_Texture *texture);
};
}
#endif
Loading…
Cancel
Save