I have recently learnt about RAII and exceptions and I have been trying to implement them into my code, but I have met an impasse.
With this code here what I am trying to do is manage everything that is being created so that there is nothing left over for when the program ends. I have deliberately set it so that media throws an exception. But before I continue on with more code, I am wondering if this general way of doing things is correct and if are there any other/better approaches to what I am doing.
The two classes that confuse me most are Media
and Game
. In their destructor, they have it stated that when they go out of scope. They should inform the console that they have gone out of scope, but it isn't currently doing that.
main.cpp
#include "Enemy.h"
#include "Media.h"
#include "Window.h"
#include "Game.h"
#include <SDL.h>
#include <iostream>
int main(int argc, char *args[])
{
try
{
Window window;
Media media;
Game game;
}
catch (const std::exception &e)
{
std::cout << e.what();
std::cout << "\nProgram failed.";
}
char c;
std::cin >> c;
return 0;
}
Window.h
#pragma once
#include <SDL.h>
#include <memory>
#include <string>
class Window
{
public:
Window();
~Window();
SDL_Window* getWindow() const { return m_window; }
SDL_Surface* getWindowSurface() const { return m_windowSurface; }
static constexpr int SCREEN_WIDTH = 800;
static constexpr int SCREEN_HEIGHT = 800;
private:
void createWindow();
void CreateWindowSurface();
SDL_Window* m_window = nullptr;
SDL_Surface* m_windowSurface = nullptr;
};
Window.cpp
#include "Window.h"
#include <iostream>
Window::Window()
{
createWindow();
CreateWindowSurface();
}
Window::~Window()
{
SDL_DestroyWindow(m_window);
std::cout << "Window destroyed.\n";
}
void Window::createWindow()
{
m_window = SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
std::cout << "Window created.\n";
if (m_window == nullptr) {
throw(std::exception("Failed to initialize Window.\n"));
}
}
void Window::CreateWindowSurface()
{
m_windowSurface = SDL_GetWindowSurface(m_window);
if (m_windowSurface == nullptr) {
SDL_DestroyWindow(m_window);
throw(std::exception("Failed to get window surface.\n!"));
}
}
Media.h
#pragma once
#include <SDL.h>
class Media
{
public:
Media();
~Media();
private:
void loadSurface();
SDL_Surface* m_playerSurface = nullptr;
};
Media.cpp
#include "Media.h"
#include <iostream>
Media::Media()
{
loadSurface();
//Load all other textures that are associated with the game
}
Media::~Media()
{
std::cerr << "Media destroyed.\n";
}
void Media::loadSurface()
{
m_playerSurface = SDL_LoadBMP("Origrimmar.bmp");
if (m_playerSurface == nullptr) {
throw(std::exception("Cannot load player texture"));
}
}
Game.h
#pragma once
#include <SDL.h>
class Game
{
public:
Game();
~Game();
};
Game.cpp
#include "Game.h"
#include <iostream>
Game::Game()
{
std::cerr << "Game created.\n";
}
Game::~Game()
{
std::cerr << "Game destroyed.\n";
}