-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDLWindow.cpp
More file actions
96 lines (77 loc) · 2.73 KB
/
SDLWindow.cpp
File metadata and controls
96 lines (77 loc) · 2.73 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
/* runcpp2
Dependencies:
- Name: SDL2
Platforms: [DefaultPlatform]
Source:
Git:
URL: "https://github.com/libsdl-org/SDL.git"
Branch: "release-2.32.6"
LibraryType: Shared
IncludePaths:
- "./include"
LinkProperties:
SearchLibraryNames: ["SDL2"]
ExcludeLibraryNames: ["SDL2-static", "SDL2_test"] # On Windows
SearchDirectories: ["./build", "./build/debug"]
Setup: ["mkdir build", "cd build && cmake .. && cmake --build . -j 16"]
# Or use system installed SDL on Unix...
# OverrideLinkFlags:
# Unix:
# "g++":
# Remove: ""
# Append: "-lSDL2"
*/
#include "SDL.h"
#include <iostream>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
//Credits: https://github.com/AlmasB/SDL2-Demo/blob/master/src/Main.cpp
// https://github.com/aminosbh/basic-cpp-sdl-project/blob/master/src/main.cc
int main(int arc, char ** argv)
{
SDL_Window* window = nullptr;
if (SDL_Init( SDL_INIT_VIDEO ) < 0)
std::cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
else
{
window = SDL_CreateWindow( "SDL2 Demo",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
}
if(!window)
return 1;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!renderer)
return 1;
SDL_Rect squareRect;
//Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT)
squareRect.w = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
squareRect.h = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
//Square position: In the middle of the screen
squareRect.x = SCREEN_WIDTH / 2 - squareRect.w / 2;
squareRect.y = SCREEN_HEIGHT / 2 - squareRect.h / 2;
bool quit = false;
SDL_Event e;
while (!quit)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
quit = true;
// Initialize renderer color white for the background
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
// Set renderer color red to draw the square
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(renderer, &squareRect);
SDL_RenderPresent(renderer);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}