Skip to content

Commit 6826c9e

Browse files
author
Hugo M. Ruiz-Mireles
committed
ESC to quit; window matches native screen res
I added the option to quit the game loop with the escape key and made the window match the user's native resolution while ignoring Windows DPI.
1 parent 78375b2 commit 6826c9e

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

include/RenderWindow.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class RenderWindow
77
{
88
public:
9-
RenderWindow(const char *title, int w, int h);
9+
RenderWindow(const char *title, int w, int h, Uint32 windowFlags = SDL_WINDOW_SHOWN);
1010
SDL_Texture *loadTexture(const char *filePath);
1111
void clear();
1212
void render(Entity& entity);

src/RenderWindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#include "RenderWindow.hpp"
33

44

5-
RenderWindow::RenderWindow(const char *title, int w, int h) : window(NULL), renderer(NULL)
5+
RenderWindow::RenderWindow(const char *title, int w, int h, Uint32 windowFlags) : window(NULL), renderer(NULL)
66
{
7-
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN);
7+
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, windowFlags);
88
if (window == NULL)
99
{
1010
std::cout << "Window failed to init. ERROR: " << SDL_GetError() << std::endl;

src/meowstro.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
2+
#include <Windows.h>
23
#include <string>
4+
#include <SDL_ttf.h>
35
#include "RenderWindow.hpp"
46
#include "Entity.hpp"
57

@@ -9,8 +11,17 @@ int main(int argc, char *args[])
911
std::cout << "SDL_Init has failed, SDL ERROR: " << SDL_GetError();
1012
if (!(IMG_Init(IMG_INIT_PNG)))
1113
std::cout << "IMG_Init has failed, SDL ERROR: " << SDL_GetError();
12-
13-
RenderWindow window("GAME v1.0", 1280, 720);
14+
15+
SetProcessDPIAware();
16+
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
17+
18+
int nativeWidth = GetSystemMetrics(SM_CXSCREEN);
19+
int nativeHeight = GetSystemMetrics(SM_CYSCREEN);
20+
21+
int windowWidth = nativeWidth * 0.9;
22+
int windowHeight = nativeHeight * 0.9;
23+
24+
RenderWindow window("Meowstro v1.0", windowWidth, windowHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
1425

1526
#ifdef CI_BUILD
1627
SDL_Delay(5000);
@@ -40,7 +51,6 @@ int main(int argc, char *args[])
4051
Entity(1092, 250, fishTextures[6]),
4152
Entity(1092, 90, fishTextures[7]) };
4253

43-
4454
bool gameRunning = true;
4555

4656
SDL_Event event;
@@ -49,9 +59,18 @@ int main(int argc, char *args[])
4959
{
5060
while (SDL_PollEvent(&event))
5161
{
52-
if (event.type == SDL_QUIT)
62+
switch(event.type)
5363
{
64+
case SDL_QUIT:
5465
gameRunning = false;
66+
break;
67+
case SDL_KEYDOWN:
68+
switch (event.key.keysym.sym)
69+
{
70+
case SDLK_ESCAPE:
71+
gameRunning = false;
72+
break;
73+
}
5574
}
5675
}
5776
window.clear();
@@ -60,12 +79,13 @@ int main(int argc, char *args[])
6079
{
6180
window.render(fishEntities[i]);
6281
}
63-
82+
6483
window.display();
6584
}
6685

6786
window.~RenderWindow();
6887
SDL_Quit();
6988

89+
7090
return 0;
7191
}

0 commit comments

Comments
 (0)