Browse Source

Implemented part of a SDL video class.

* Removed test "Hello" class.
    * Corrected SDL2 library declaration.
master
Ian Burgmyer 4 years ago
parent
commit
bb8d2c48a0
  1. 5
      CMakeLists.txt
  2. 5
      libplip/Hello.cpp
  3. 8
      libplip/Hello.h
  4. 6
      libplip/Plip.cpp
  5. 10
      libplip/Plip.h
  6. 20
      libplip/PlipVideo.h
  7. 17
      libplip/PlipVideoException.h
  8. 73
      plip-sdl/SdlWindow.cpp
  9. 35
      plip-sdl/SdlWindow.h
  10. 6
      plip-sdl/main.cpp

5
CMakeLists.txt

@ -24,7 +24,7 @@ add_custom_target(
)
add_library(${lib_name}
libplip/Hello.cpp
libplip/Plip.cpp
)
add_dependencies(${lib_name} GENERATE_LIB_VERSION_HEADER)
@ -44,6 +44,7 @@ add_custom_target(
add_executable(${gui_name}
plip-sdl/main.cpp
plip-sdl/SdlWindow.cpp
)
target_include_directories(${gui_name}
@ -53,7 +54,7 @@ target_include_directories(${gui_name}
target_link_libraries(${gui_name}
plip
${SDL2_INCLUDE_DIRS}
${SDL2_LIBRARIES}
)
add_dependencies(${gui_name} GENERATE_CLI_VERSION_HEADER)

5
libplip/Hello.cpp

@ -1,5 +0,0 @@
#include "Hello.h"
int Hello::Calculate() {
return 42;
}

8
libplip/Hello.h

@ -1,8 +0,0 @@
#pragma once
class Hello {
public:
static int Calculate();
};

6
libplip/Plip.cpp

@ -0,0 +1,6 @@
/* Plip.cpp
*
* The main class for controlling the Plip emulation suite.
*/
#include "Plip.h"

10
libplip/Plip.h

@ -0,0 +1,10 @@
/* Plip.h
*
* The main class for controlling the Plip emulation suite.
*/
#pragma once
class Plip {
};

20
libplip/PlipVideo.h

@ -0,0 +1,20 @@
/* PlipVideo.h
*
* Provides a toolkit-agnostic video interface.
*/
#pragma once
#include <string>
namespace Plip {
class PlipVideo {
public:
virtual void Resize(int width, int height) = 0;
virtual void SetTitle(std::string title) = 0;
protected:
PlipVideo() = default;
~PlipVideo() = default;
};
}

17
libplip/PlipVideoException.h

@ -0,0 +1,17 @@
/* plipVideoException.h
*
* An exception that may occur during video initialization.
*/
#pragma once
#include <stdexcept>
#include <string>
#include <utility>
namespace Plip {
struct PlipVideoException : std::runtime_error {
explicit PlipVideoException(const char *message)
: std::runtime_error(message) {}
};
}

73
plip-sdl/SdlWindow.cpp

@ -0,0 +1,73 @@
/* SdlWindow.cpp
*
* Describe this file here.
*/
#include <sstream>
#include "PlipVideoException.h"
#include "SdlWindow.h"
namespace PlipSdl {
SdlWindow::SdlWindow(int scale, std::string title) {
std::stringstream error;
m_scale = scale;
// Try to create a window.
m_window = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
m_width * m_scale, m_height * m_scale, 0);
if(m_window == nullptr) {
error << "Unable to create SDL window: " << SDL_GetError() << "\n";
throw Plip::PlipVideoException(error.str().c_str());
}
// Try to create a renderer.
m_renderer = SDL_CreateRenderer(m_window, -1, 0);
if(m_renderer == nullptr) {
error << "Unable to create SDL renderer: " << SDL_GetError() << "\n";
throw Plip::PlipVideoException(error.str().c_str());
}
// Try to create a small starting texture. This will be removed and
// recreated later, but we want to ensure that we can make one in the
// first place.
m_texture = SDL_CreateTexture(m_renderer, 0, 0, m_width, m_height);
if(m_texture == nullptr) {
error << "Unable to create SDL texture: " << SDL_GetError() << "\n";
throw Plip::PlipVideoException(error.str().c_str());
}
}
SdlWindow::~SdlWindow() {
if(m_texture != nullptr) SDL_DestroyTexture(m_texture);
if(m_renderer != nullptr) SDL_DestroyRenderer(m_renderer);
if(m_window != nullptr) SDL_DestroyWindow(m_window);
}
void SdlWindow::Resize(int width, int height) {
m_width = width;
m_height = height;
// Resize the window.
SDL_SetWindowSize(m_window, m_width * m_scale, m_height * m_scale);
// Destroy and recreate the texture.
SDL_DestroyTexture(m_texture);
SDL_CreateTexture(m_renderer, 0, 0, m_width, m_height);
}
void SdlWindow::SetScale(int scale) {
m_scale = scale;
SDL_SetWindowSize(m_window, m_width * m_scale, m_height * m_scale);
}
void SdlWindow::SetTitle(std::string title) {
SDL_SetWindowTitle(m_window, title.c_str());
}
}

35
plip-sdl/SdlWindow.h

@ -0,0 +1,35 @@
/* SdlWindow.h
*
* Describe this file here.
*/
#pragma once
#include <SDL.h>
#include "PlipVideo.h"
namespace PlipSdl {
class SdlWindow : Plip::PlipVideo {
public:
SdlWindow(int scale = 1, std::string title = "");
~SdlWindow();
void Resize(int width, int height);
void SetScale(int scale);
void SetTitle(std::string title);
private:
const int m_init_width = 64;
const int m_init_height = 64;
int m_width = m_init_width;
int m_height = m_init_height;
SDL_Window *m_window = nullptr;
SDL_Renderer *m_renderer = nullptr;
SDL_Texture *m_texture = nullptr;
int m_scale;
};
}

6
plip-sdl/main.cpp

@ -7,6 +7,7 @@
#include "cxxopts.hpp"
#include "SdlWindow.h"
#include "version.h"
cxxopts::ParseResult parseCmdLine(int argc, char **argv) {
@ -78,10 +79,7 @@ int main(int argc, char **argv) {
return 0;
}
std::cout << "scale: " << opts["scale"].as<int>()
<< "\ncore: " << opts["core"].as<std::string>()
<< "\nfile: " << opts["filename"].as<std::string>()
<< std::endl;
auto wnd = new PlipSdl::SdlWindow(opts["scale"].as<int>(), windowTitle);
return 0;
}

Loading…
Cancel
Save