Fixed assertion failures

This commit is contained in:
Robert 2020-05-16 20:49:04 +02:00
parent a57f81e0b3
commit b9e8fca5cc
3 changed files with 51 additions and 3 deletions

View file

@ -5,11 +5,15 @@ namespace sdlu
{
RenderWindow::RenderWindow()
{
m_pWindow = nullptr;
m_pRenderer = nullptr;
}
RenderWindow::RenderWindow(Vector2u dimension, const std::string& title,
Uint32 windowFlags, Uint32 rendererFlags)
{
m_pWindow = nullptr;
m_pRenderer = nullptr;
Create(dimension, title, windowFlags, rendererFlags);
}
@ -21,6 +25,9 @@ namespace sdlu
void RenderWindow::Create(Vector2u dimension, const std::string& title,
Uint32 windowFlags, Uint32 rendererFlags)
{
// Don't create a window when it already exists
if (!IS_NULLPTR(m_pWindow)) return;
m_pWindow = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
dimension.x, dimension.y,
@ -37,14 +44,21 @@ namespace sdlu
void RenderWindow::Close()
{
// Don't destroy a window that doesn't exist
if (IS_NULLPTR(m_pWindow)) return;
SDL_DestroyRenderer(m_pRenderer);
m_pRenderer = nullptr;
SDL_DestroyWindow(m_pWindow);
m_pWindow = nullptr;
OnClose();
}
bool RenderWindow::IsOpen()
{
return !IS_NULLPTR(m_pWindow);
return (!SDL_GetWindowID(m_pWindow) ? false : true);
}
bool RenderWindow::PollEvent(SDL_Event* event)
@ -89,8 +103,8 @@ namespace sdlu
Vector2u RenderWindow::GetSize()
{
unsigned int x = 0, y = 0;
SDL_GetWindowSize(m_pWindow, x, y);
int x = 0, y = 0;
SDL_GetWindowSize(m_pWindow, &x, &y);
return Vector2u(x, y);
}
@ -103,4 +117,14 @@ namespace sdlu
{
SDL_SetWindowSize(m_pWindow, width, height);
}
std::string RenderWindow::GetTitle()
{
return SDL_GetWindowTitle(m_pWindow);
}
void RenderWindow::SetTitle(std::string title)
{
SDL_SetWindowTitle(m_pWindow, title.c_str());
}
}

View file

@ -126,6 +126,21 @@ namespace sdlu
*/
void SetSize(unsigned int width, unsigned int height);
/**
* @brief Gets the current window title
*
* @return The title of the widnow
*/
std::string GetTitle();
/**
* @brief Sets a new window title
*
* @param[in] title The new window title
*/
void SetTitle(std::string title);
protected:
SDL_Window* m_pWindow; ///< A pointer to the window object
SDL_Renderer* m_pRenderer; ///< A pointer to the renderer object

View file

@ -2,6 +2,8 @@
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
sdlu::Vector2f vecA(.4f, -2.3f);
sdlu::Vector2f vecB(-8.9f, 0.003f);
sdlu::Vector2f vec = vecA + vecB;
@ -17,7 +19,14 @@ int main(int argc, char** argv)
while (window.WaitEvent(&event))
{
std::cout << event.type << std::endl;
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
{
window.Close();
break;
}
}
SDL_Quit();
return 0;
}