-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
167 lines (129 loc) · 4.65 KB
/
window.cpp
File metadata and controls
167 lines (129 loc) · 4.65 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<iostream>
#include<SDL.h>
#include<string>
#include "main.h"
#include "window.h"
int statusCode = 0; //0 = good, 1 = sdl_init faild
int windowSizeIn = 8, pixelSizeX = 3, pixelSizeY = 3;
float targetFPS = 60;
const int gameArrayX = 256, gameArrayY = 240;
int windowWidth = gameArrayX * pixelSizeX, windowHeight = gameArrayY * pixelSizeY;
SDL_Window* windowMain;
Uint32 windowMainID = NULL;
SDL_Renderer* rendererMain;
SDL_Rect pixelRectArray[gameArrayX * gameArrayY];
pos mouse;
Uint32 mouseButtons;
const Uint8* keyboardState;
void initSDL() {
//initilize needed SDL functions
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
statusCode = 1;
programRunning = false;
}
/*
*
* float dpiH, dpiV, dpiD;
*
if (SDL_GetDisplayDPI(0, &dpiD, &dpiH, &dpiV) != 0) {
SDL_Log("Unable to get display dpi: %s", SDL_GetError());
}
else {
pixelSizeX = (int)((dpiH * windowSizeIn) / gameArrayX);
pixelSizeY = (int)((dpiH * windowSizeIn) / gameArrayY);
windowWidth = gameArrayX * pixelSizeX;
windowHeight = gameArrayY * pixelSizeY;
SDL_Log("Display DPI: %f", dpiD);
}
*/
windowMain = SDL_CreateWindow(
"Pong v0.3.2-alpha", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
windowWidth, // width, in pixels
windowHeight, // height, in pixels
NULL // flags
);
if (windowMain == NULL) { // Check that the window was successfully created
SDL_Log("Could not create window: %s\n", SDL_GetError());
statusCode = 1;
programRunning = false;
}
else {
windowMainID = SDL_GetWindowID(windowMain);
SDL_Log("Window Resoultion: %i , %i", windowWidth, windowHeight);
}
rendererMain = SDL_CreateRenderer(windowMain, -1, SDL_RENDERER_ACCELERATED);
if (rendererMain == NULL) { //Check that thr renderer was successfully created
SDL_Log("Could not create renderer: %s\n", SDL_GetError());
statusCode = 1;
programRunning = false;
}
}
void checkInputs() {
mouseButtons = SDL_GetMouseState(&mouse.x, &mouse.y);
keyboardState = SDL_GetKeyboardState(NULL);
if (keyboardState[SDL_SCANCODE_ESCAPE]) {
currentMenu = Screen::MainMenu;
gameReset();
scoreReset();
}
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT: {
if (event.window.windowID == windowMainID) {
switch (event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED: {
windowWidth = event.window.data1;
windowHeight = event.window.data2;
break;
}
case SDL_WINDOWEVENT_CLOSE: {
event.type = SDL_QUIT;
SDL_PushEvent(&event);
break;
}
}
}
break;
}
case SDL_QUIT: {
programRunning = false;
break;
}
}
}
}
void gameDraw() {
int pixelRectArrayIndex = 0;
for (int y = 0; y < gameArrayY; y++) {
for (int x = 0; x < gameArrayX; x++) {
if (x >= ball.x && x < ball.x + ball.w && y >= ball.y && y < ball.y + ball.h
|| x >= paddle1.x && x < paddle1.x + paddle1.w && y >= paddle1.y && y < paddle1.y + paddle1.h
|| x >= paddle2.x && x < paddle2.x + paddle2.w && y >= paddle2.y && y < paddle2.y + paddle2.h
) {
pixelRect.x = x * pixelSizeX;
pixelRect.y = y * pixelSizeY;
pixelRectArray[pixelRectArrayIndex] = pixelRect;
pixelRectArrayIndex++;
}
}
}
SDL_SetRenderDrawColor(rendererMain, 31, 31, 33, NULL);
SDL_RenderClear(rendererMain);
SDL_SetRenderDrawColor(rendererMain, 89, 89, 94, NULL);
textToScreen(std::to_string(player1Score), 150, 100, 16);
textToScreen(std::to_string(player2Score), 550, 100, 16);
SDL_RenderFillRects(rendererMain, pixelRectArray, pixelRectArrayIndex);
SDL_RenderPresent(rendererMain);
}
void cleanUp() {
programRunning = false;
if(!spriteSheetSurface) SDL_FreeSurface(spriteSheetSurface);
if(!spriteSheetTexture) SDL_DestroyTexture(spriteSheetTexture);
if(!rendererMain) SDL_DestroyRenderer(rendererMain);
if(!windowMain)SDL_DestroyWindow(windowMain);
SDL_Quit();
}