Skip to content

Commit

Permalink
decouple VgaTerminal drawing functionality to Window. #122
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffaello committed May 16, 2020
1 parent c82633e commit ec37636
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 deletions.
12 changes: 5 additions & 7 deletions sdl2-vga-terminal/src/VgaTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void VgaTerminal::_renderFontChar(const SDL_Point& dst, _terminalChar_t& tc)

void VgaTerminal::_renderCharLine(const std::bitset<8>& line, const int dstx, const int dsty, const uint8_t col, const uint8_t bgCol)
{
// lsz is mode.cw,
// start make sense spliting to a VgaTerminalRender, all the
// rendering functions.
// TODO lsz is mode.cw,
// BODY start make sense spliting to a VgaTerminalRender, all the
// BODY rendering functions.
constexpr auto lsz = 8;
SDL_Point points[lsz];
uint8_t fgi = 0;
Expand All @@ -114,10 +114,8 @@ void VgaTerminal::_renderCharLine(const std::bitset<8>& line, const int dstx, co
else points[--bgi] = { dstx - x, dsty };
}

SDL_SetRenderDrawColor(getRenderer(), _pal.colors[col].r, _pal.colors[col].g, _pal.colors[col].b, _pal.colors[col].a);
SDL_RenderDrawPoints(getRenderer(), points, fgi);
SDL_SetRenderDrawColor(getRenderer(), _pal.colors[bgCol].r, _pal.colors[bgCol].g, _pal.colors[bgCol].b, _pal.colors[bgCol].a);
SDL_RenderDrawPoints(getRenderer(), &points[fgi], lsz - bgi);
renderDrawPoints(_pal.colors[col], points, fgi);
renderDrawPoints(_pal.colors[bgCol], &points[fgi], lsz - bgi);
}

void VgaTerminal::_renderCursor(const SDL_Point& dst, const _terminalChar_t& tc)
Expand Down
21 changes: 14 additions & 7 deletions sdl2-vga-terminal/src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,44 @@ Window::Window(const std::string& title, const int x, const int y, const int wid
renderPresent();
}

SDL_Window* Window::getWindow() const
SDL_Window* Window::getWindow() const noexcept
{
return _pWindow;
}

SDL_Renderer* Window::getRenderer() const {
SDL_Renderer* Window::getRenderer() const noexcept
{
return _pRenderer;
}

void Window::setRendererDrawColor(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) const
void Window::setRendererDrawColor(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) const noexcept
{
SDL_SetRenderDrawColor(_pRenderer, r, g, b, a);
}

void Window::renderClear() const
void Window::renderDrawPoints(const SDL_Color& color, const SDL_Point points[], int count) const noexcept
{
SDL_SetRenderDrawColor(getRenderer(), color.r, color.g, color.b, color.a);
SDL_RenderDrawPoints(getRenderer(), points, count);
}

void Window::renderClear() const noexcept
{
SDL_RenderClear(_pRenderer);
}

void Window::renderPresent() const
void Window::renderPresent() const noexcept
{
SDL_RenderPresent(_pRenderer);
}

void Window::toggleFullscreenDesktop()
void Window::toggleFullscreenDesktop() noexcept
{
_bFullScreenDesktop = !_bFullScreenDesktop;
SDL_SetWindowFullscreen(getWindow(), _bFullScreenDesktop ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
}

uint32_t Window::getWindowId() const
uint32_t Window::getWindowId() const noexcept
{
return _windowId;
}
Expand Down
16 changes: 9 additions & 7 deletions sdl2-vga-terminal/src/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class Window
Window& operator=(const Window&&) = delete;
Window(const std::string &title, const int width, const int height, const int winFlags, const int drvIndex, const int renFlags);
Window(const std::string& title, const int x, const int y, const int width, const int height, const int winFlags, const int drvIndex, const int renFlags);
SDL_Window* getWindow() const;
SDL_Renderer* getRenderer() const;
SDL_Window* getWindow() const noexcept;
SDL_Renderer* getRenderer() const noexcept;

void setRendererDrawColor(const uint8_t r,const uint8_t g, const uint8_t b, const uint8_t a) const;
void renderClear() const;
void renderPresent() const;
void toggleFullscreenDesktop();
void setRendererDrawColor(const uint8_t r,const uint8_t g, const uint8_t b, const uint8_t a) const noexcept;
void renderDrawPoints(const SDL_Color& color, const SDL_Point points[], int count) const noexcept;

uint32_t getWindowId() const;
void renderClear() const noexcept;
void renderPresent() const noexcept;
void toggleFullscreenDesktop() noexcept;

uint32_t getWindowId() const noexcept;

typedef std::function<bool(SDL_Event&, Window*)> handler_t;
handler_t handler = nullptr;
Expand Down
9 changes: 9 additions & 0 deletions sdl2-vga-terminal/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ if (TEST_DUMP_SNAPSHOT)
set (BUILD_SNAPSHOT bool ON) # ON by dependent option configuration anyway, but not hurting
add_compile_definitions(TEST_DUMP_SNAPSHOT)
else()
### Window tests ###
add_executable(runWindowTests "WindowTest.cpp")
add_test(WindowTest runWindowTests)
target_link_libraries(runWindowTests PRIVATE ${LIB_SDL2main} vga-terminal-static GTest::gtest GTest::gmock)
add_custom_command(TARGET runWindowTests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:GTest::gtest_main> $<TARGET_FILE_DIR:runWindowTests>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:GTest::gtest> $<TARGET_FILE_DIR:runWindowTests>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:GTest::gmock> $<TARGET_FILE_DIR:runWindowTests>
)
### VgaTerminal tests ###
add_executable(runVgaTerminalTests "VgaTerminalTest.cpp")
add_test(VgaTerminalTest runVgaTerminalTests)
Expand Down
1 change: 0 additions & 1 deletion sdl2-vga-terminal/test/VgaTerminalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ TEST(VgaTerminal, fill)
cmpTerminalChar(term.at(0, 1), VgaTerminal::terminalChar_t({ 0, 0, 0 }));
}


TEST(VgaTerminal, fillRestoreAndNoAutoScroll)
{
std::string title = ::testing::UnitTest::GetInstance()->current_test_info()->name();
Expand Down
21 changes: 21 additions & 0 deletions sdl2-vga-terminal/test/WindowTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <Window.hpp>
#include <SDL2/SDL_image.h>
#include <cmath>
#include "Environment.hpp"
#include <cstring>

TEST(Window, cannotInit)
{
Environment::tearDown();
ASSERT_THROW(Window term = Window("", 320, 200, 0, -1, 0), std::runtime_error);
ASSERT_THROW(Window term = Window("", 0, 0, 320, 200, 0, -1, 0), std::runtime_error);
Environment::setUp();
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
::testing::AddGlobalTestEnvironment(new Environment());
return RUN_ALL_TESTS();
}

0 comments on commit ec37636

Please sign in to comment.