Skip to content

Commit ec3f14d

Browse files
committed
Add week 3 projects
1 parent 3778195 commit ec3f14d

File tree

250 files changed

+74103
-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.

250 files changed

+74103
-0
lines changed

Engine/BaseGame.cpp

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

Engine/BaseGame.h

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

0 commit comments

Comments
 (0)