-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderWindow.hpp
More file actions
110 lines (87 loc) · 3.03 KB
/
RenderWindow.hpp
File metadata and controls
110 lines (87 loc) · 3.03 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
102
103
104
105
106
107
108
109
110
#ifndef RENDERWINDOW_HPP
#define RENDERWINDOW_HPP
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include "Game.hpp"
using namespace std;
class RenderWindow{
public:
RenderWindow (const char* title_in, int width, int height) : window(nullptr), renderer(nullptr){
window = SDL_CreateWindow(title_in, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
if (window == nullptr)
cout << "WINDOW FAILED TO INIT WITH ERROR: " << SDL_GetError() << endl;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
game.setRenderer(renderer);
}
SDL_Texture* loadTexture(const char* file_path){
SDL_Texture* texture = nullptr;
texture = IMG_LoadTexture(renderer, file_path);
if (!texture)
cout << "Failed to load the texture at " << file_path << " with error: " << SDL_GetError() << endl;
return texture;
}
// Handles mouse down presses, quit game, etc
void handleEvents(){
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_MOUSEMOTION:
mouse_pos = {event.motion.x, event.motion.y};
if (left_mouse_button_down){
game.update_mouse_pos(mouse_pos);
}
break;
case SDL_MOUSEBUTTONUP:
if (left_mouse_button_down && event.button.button == SDL_BUTTON_LEFT){
left_mouse_button_down = false;
game.end_dragging(mouse_pos);
}
break;
case SDL_MOUSEBUTTONDOWN:
if (!left_mouse_button_down && event.button.button == SDL_BUTTON_LEFT){
left_mouse_button_down = true;
game.start_dragging(mouse_pos);
}
break;
case SDL_KEYDOWN:
printf("The %s key was pressed!\n", SDL_GetKeyName(event.key.keysym.sym));
break;
case SDL_QUIT:
cout << "Thanks for playing!!!" << endl;
this->is_running = false;
break;
default:
break;
}
}
void render(){
game.render();
display();
}
void display(){
SDL_RenderPresent(renderer);
}
void update(){
game.update();
}
void clear(){
SDL_RenderClear(renderer);
}
void cleanUp(){
SDL_DestroyWindow(window);
}
bool running() {
return this->is_running;
}
private:
bool is_running = true;
bool left_mouse_button_down = false;
SDL_Point mouse_pos;
SDL_Point click_offset;
SDL_Window* window;
SDL_Renderer* renderer;
Game game;
};
#endif