-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.hpp
More file actions
95 lines (86 loc) · 2.59 KB
/
Copy pathRenderer.hpp
File metadata and controls
95 lines (86 loc) · 2.59 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
#pragma once
#include "Coordinate.hpp"
#include <SDL.h>
#include <string>
#include <array>
#include <vector>
#include <stdexcept>
#include <chrono>
class Renderer {
public:
Renderer(int width = 640, int height = 480, const std::string& title = "Window");
~Renderer();
void run();
virtual void initialise();
virtual void update(double frame_time);
virtual void close();
protected:
struct Pixel {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha = 255;
};
static constexpr Pixel white = { 255, 255, 255 };
void clear(const Pixel & = { 0, 0, 0 });
void draw_pixel(const Coordinate&, const Pixel & = white);
void draw_line(const Coordinate&, const Coordinate&, const Pixel & = white);
void draw_triangle(std::array<Coordinate, 3>, const Pixel & = white);
void draw_filled_triangle(std::array<Coordinate, 3>, const Pixel & = white);
void draw_rectangle(const Coordinate&, const Coordinate&, const Pixel & = white);
void draw_circle(const Coordinate&, int radius, const Pixel & = white);
void sleep(int milliseconds);
int width() const { return _width; }
int height() const { return _height; }
bool _running = true;
enum class ButtonState {
Pressed,
Held,
Released
};
enum class MouseButton {
Left,
Middle,
Right
};
Coordinate mouse_position() const;
ButtonState mouse_button(MouseButton) const;
enum class Key : SDL_Keycode {
Left = SDLK_LEFT,
Right = SDLK_RIGHT,
Up = SDLK_UP,
Down = SDLK_DOWN,
Space = SDLK_SPACE,
Escape = SDLK_ESCAPE,
Control = SDLK_LCTRL,
};
ButtonState key(Key) const;
ButtonState key(char) const;
private:
void handle_events();
void render();
const int _width;
const int _height;
SDL_Window* _window = nullptr;
SDL_Renderer* _renderer = nullptr;
SDL_Texture* _screen = nullptr;
std::vector<std::vector<Pixel>> _pixels;
double _time_elapsed = 0;
Coordinate _mouse_position;
std::array<ButtonState, 5> _mouse_buttons;
std::array<ButtonState, 128> _keys;
};
class SDLException final : public std::runtime_error {
public:
explicit SDLException(const std::string& message) : std::runtime_error(message + "(SDL_Error: " + std::string(SDL_GetError()) + ")\n") {}
};
namespace Timer {
void start();
void stop();
double elapsed();
void restart();
static struct {
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point stop;
} time;
};