Skip to content

Commit 9689f95

Browse files
committed
Add week 6 projects
1 parent 3778195 commit 9689f95

File tree

256 files changed

+74932
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+74932
-0
lines changed

Engine/BaseGame.cpp

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// Knapecz, Adam - 1DAE11
2+
#include "base.h"
3+
#include <iostream>
4+
#include <algorithm>
5+
#include <chrono>
6+
#include "BaseGame.h"
7+
8+
BaseGame::BaseGame(const Window& window)
9+
: m_Window{ window }
10+
, m_Viewport{ 0,0,window.width,window.height }
11+
, m_pWindow{ nullptr }
12+
, m_pContext{ nullptr }
13+
, m_Initialized{ false }
14+
, m_MaxElapsedSeconds{ 0.1f }
15+
{
16+
InitializeGameEngine();
17+
}
18+
19+
BaseGame::~BaseGame()
20+
{
21+
CleanupGameEngine();
22+
}
23+
24+
void BaseGame::InitializeGameEngine()
25+
{
26+
// disable console close window button
27+
#ifdef _WIN32
28+
HWND hwnd = GetConsoleWindow();
29+
HMENU hmenu = GetSystemMenu(hwnd, FALSE);
30+
EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED);
31+
#endif
32+
33+
// Initialize SDL
34+
if (SDL_Init(SDL_INIT_VIDEO /*| SDL_INIT_AUDIO*/) < 0)
35+
{
36+
std::cerr << "BaseGame::Initialize( ), error when calling SDL_Init: " << SDL_GetError() << std::endl;
37+
return;
38+
}
39+
40+
// Use OpenGL 2.1
41+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
42+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
43+
44+
// Create window
45+
m_pWindow = SDL_CreateWindow(
46+
m_Window.title.c_str(),
47+
SDL_WINDOWPOS_CENTERED,
48+
SDL_WINDOWPOS_CENTERED,
49+
int(m_Window.width),
50+
int(m_Window.height),
51+
SDL_WINDOW_OPENGL);
52+
if (m_pWindow == nullptr)
53+
{
54+
std::cerr << "BaseGame::Initialize( ), error when calling SDL_CreateWindow: " << SDL_GetError() << std::endl;
55+
return;
56+
}
57+
58+
// Create OpenGL context
59+
m_pContext = SDL_GL_CreateContext(m_pWindow);
60+
if (m_pContext == nullptr)
61+
{
62+
std::cerr << "BaseGame::Initialize( ), error when calling SDL_GL_CreateContext: " << SDL_GetError() << std::endl;
63+
return;
64+
}
65+
66+
// Set the swap interval for the current OpenGL context,
67+
// synchronize it with the vertical retrace
68+
if (m_Window.isVSyncOn)
69+
{
70+
if (SDL_GL_SetSwapInterval(1) < 0)
71+
{
72+
std::cerr << "BaseGame::Initialize( ), error when calling SDL_GL_SetSwapInterval: " << SDL_GetError() << std::endl;
73+
return;
74+
}
75+
}
76+
else
77+
{
78+
SDL_GL_SetSwapInterval(0);
79+
}
80+
81+
// Set the Projection matrix to the identity matrix
82+
glMatrixMode(GL_PROJECTION);
83+
glLoadIdentity();
84+
85+
// Set up a two-dimensional orthographic viewing region.
86+
glOrtho(0, m_Window.width, 0, m_Window.height, -1, 1); // y from bottom to top
87+
88+
// Set the viewport to the client window area
89+
// The viewport is the rectangular region of the window where the image is drawn.
90+
glViewport(0, 0, int(m_Window.width), int(m_Window.height));
91+
92+
// Set the Modelview matrix to the identity matrix
93+
glMatrixMode(GL_MODELVIEW);
94+
glLoadIdentity();
95+
96+
// Enable color blending and use alpha blending
97+
glEnable(GL_BLEND);
98+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99+
100+
// Initialize PNG loading
101+
/*
102+
int imgFlags = IMG_INIT_PNG;
103+
if ( !( IMG_Init( imgFlags ) & imgFlags ) )
104+
{
105+
std::cerr << "BaseGame::Initialize( ), error when calling IMG_Init: " << IMG_GetError( ) << std::endl;
106+
return;
107+
}
108+
*/
109+
110+
// Initialize SDL_ttf
111+
if (TTF_Init() == -1)
112+
{
113+
std::cerr << "BaseGame::Initialize( ), error when calling TTF_Init: " << TTF_GetError() << std::endl;
114+
return;
115+
}
116+
117+
//Initialize SDL_mixer
118+
/*
119+
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
120+
{
121+
std::cerr << "BaseGame::Initialize( ), error when calling Mix_OpenAudio: " << Mix_GetError() << std::endl;
122+
return;
123+
}
124+
*/
125+
126+
m_Initialized = true;
127+
}
128+
129+
void BaseGame::Run()
130+
{
131+
if (!m_Initialized)
132+
{
133+
std::cerr << "BaseGame::Run( ), BaseGame not correctly initialized, unable to run the BaseGame\n";
134+
std::cin.get();
135+
return;
136+
}
137+
138+
// Main loop flag
139+
bool quit{ false };
140+
141+
// Set start time
142+
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
143+
144+
//The event loop
145+
SDL_Event e{};
146+
while (!quit)
147+
{
148+
// Poll next event from queue
149+
while (SDL_PollEvent(&e) != 0)
150+
{
151+
// Handle the polled event
152+
switch (e.type)
153+
{
154+
case SDL_QUIT:
155+
quit = true;
156+
break;
157+
case SDL_KEYDOWN:
158+
this->ProcessKeyDownEvent(e.key);
159+
break;
160+
case SDL_KEYUP:
161+
this->ProcessKeyUpEvent(e.key);
162+
break;
163+
case SDL_MOUSEMOTION:
164+
e.motion.y = int(m_Window.height) - e.motion.y;
165+
this->ProcessMouseMotionEvent(e.motion);
166+
break;
167+
case SDL_MOUSEBUTTONDOWN:
168+
e.button.y = int(m_Window.height) - e.button.y;
169+
this->ProcessMouseDownEvent(e.button);
170+
break;
171+
case SDL_MOUSEBUTTONUP:
172+
e.button.y = int(m_Window.height) - e.button.y;
173+
this->ProcessMouseUpEvent(e.button);
174+
break;
175+
}
176+
}
177+
178+
if (!quit)
179+
{
180+
// Get current time
181+
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
182+
183+
// Calculate elapsed time
184+
float elapsedSeconds = std::chrono::duration<float>(t2 - t1).count();
185+
186+
// Update current time
187+
t1 = t2;
188+
189+
// Prevent jumps in time caused by break points
190+
elapsedSeconds = std::min(elapsedSeconds, m_MaxElapsedSeconds);
191+
192+
// Call the BaseGame object 's Update function, using time in seconds (!)
193+
this->Update(elapsedSeconds);
194+
195+
// Draw in the back buffer
196+
this->Draw();
197+
198+
// Update screen: swap back and front buffer
199+
SDL_GL_SwapWindow(m_pWindow);
200+
}
201+
}
202+
}
203+
204+
void BaseGame::CleanupGameEngine()
205+
{
206+
SDL_GL_DeleteContext(m_pContext);
207+
208+
SDL_DestroyWindow(m_pWindow);
209+
m_pWindow = nullptr;
210+
211+
//Quit SDL subsystems
212+
Mix_Quit();
213+
TTF_Quit();
214+
SDL_Quit();
215+
216+
// enable console close window button
217+
#ifdef _WIN32
218+
HWND hwnd = GetConsoleWindow();
219+
HMENU hmenu = GetSystemMenu(hwnd, FALSE);
220+
EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED);
221+
#endif
222+
223+
}

Engine/BaseGame.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Knapecz, Adam - 1DAE11
2+
#pragma once
3+
#include "structs.h"
4+
#include "SDL.h"
5+
// https://BaseGameprogrammingpatterns.com/subclass-sandbox.html
6+
7+
8+
class BaseGame
9+
{
10+
public:
11+
explicit BaseGame( const Window& window );
12+
BaseGame( const BaseGame& other ) = delete;
13+
BaseGame& operator=( const BaseGame& other ) = delete;
14+
BaseGame(BaseGame&& other) = delete;
15+
BaseGame& operator=(BaseGame&& other) = delete;
16+
virtual ~BaseGame( );
17+
18+
void Run( );
19+
20+
virtual void Update(float elapsedSec)
21+
{
22+
23+
}
24+
virtual void Draw() const
25+
{
26+
27+
}
28+
29+
// Event handling
30+
virtual void ProcessKeyDownEvent(const SDL_KeyboardEvent& e)
31+
{
32+
33+
}
34+
virtual void ProcessKeyUpEvent(const SDL_KeyboardEvent& e)
35+
{
36+
37+
}
38+
virtual void ProcessMouseMotionEvent(const SDL_MouseMotionEvent& e)
39+
{
40+
41+
}
42+
virtual void ProcessMouseDownEvent(const SDL_MouseButtonEvent& e)
43+
{
44+
45+
}
46+
virtual void ProcessMouseUpEvent(const SDL_MouseButtonEvent& e)
47+
{
48+
49+
}
50+
51+
const Rectf& GetViewPort() const
52+
{
53+
return m_Viewport;
54+
}
55+
56+
private:
57+
// DATA MEMBERS
58+
// The window properties
59+
const Window m_Window;
60+
const Rectf m_Viewport;
61+
// The window we render to
62+
SDL_Window* m_pWindow;
63+
// OpenGL context
64+
SDL_GLContext m_pContext;
65+
// Init info
66+
bool m_Initialized;
67+
// Prevent timing jumps when debugging
68+
const float m_MaxElapsedSeconds;
69+
70+
// FUNCTIONS
71+
void InitializeGameEngine( );
72+
void CleanupGameEngine( );
73+
};

0 commit comments

Comments
 (0)