Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (79 loc) · 3 KB
/
Copy pathmain.cpp
File metadata and controls
101 lines (79 loc) · 3 KB
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
#include <cstdint>
#include <cstdio>
#include <cassert>
const uint32_t SCREEN_WIDTH = 700;
const uint32_t SCREEN_HEIGHT = 600;
#define ASSERT(cond, ...) if(!(cond)) { fprintf(stderr, __VA_ARGS__); exit(1); }
struct Color{
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
class Shape{
protected:
uint32_t width, height, x, y;
Color *color;
public:
Shape(){ x = 0; y = 0; color = nullptr; };
void set_values(uint32_t, uint32_t);
uint32_t getWidth() const { return width; }
uint32_t getHeight() const { return height; }
uint32_t getX() const { return x; }
uint32_t getY() const { return y; }
Color* getColor() { return color; }
};
class Rectangle : public Shape{
public:
Rectangle() : Shape(){};
void set_values(const uint32_t, const uint32_t, const uint32_t, const uint32_t, Color&);
};
void Rectangle::set_values(const uint32_t w, const uint32_t h, const uint32_t x, const uint32_t y, Color &c){
this->width = w;
this->height = h;
this->x = x;
this->y = y;
this->color = &c;
}
int main( int argc, char* args[] ){
SDL_Window* window = nullptr;
SDL_Surface* screenSurface = nullptr;
SDL_Renderer* windowRenderer = nullptr;
ASSERT(!SDL_Init(SDL_INIT_VIDEO), "ERROR, could not initialize video: %s\n", SDL_GetError());
window = SDL_CreateWindow("Testing_Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
SCREEN_HEIGHT , SDL_WINDOW_SHOWN);
ASSERT(!(window == nullptr), "ERROR, could not create window: %s\n", SDL_GetError());
//screenSurface = SDL_GetWindowSurface(window);
//ASSERT(!(screenSurface == nullptr), "ERROR, could attach window surface: %s\n", SDL_GetError());
//SDL_FillRect(screenSurface, nullptr, SDL_MapRGB(screenSurface->format, 200, 200, 200));
//SDL_UpdateWindowSurface(window);
windowRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
ASSERT(!(windowRenderer == nullptr), "ERROR, Renderer could not be created!: %s\n", SDL_GetError());
Color c = {255, 0, 0, 255};
Rectangle rect;
rect.set_values(200, 200, 100, 100, c);
SDL_Rect rectangle = { (int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight() };
Color *color = rect.getColor();
SDL_SetRenderDrawColor( windowRenderer,0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(windowRenderer);
SDL_SetRenderDrawColor( windowRenderer, color->red, color->green, color->blue, color->alpha);
SDL_RenderFillRect(windowRenderer, &rectangle);
SDL_Event e;
bool quit = false;
while( quit == false ){
while( SDL_PollEvent( &e ) ){
if( e.type == SDL_QUIT )
quit = true;
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}