diff --git a/assets/joystix/joystix.ttf b/assets/joystix/joystix.ttf
new file mode 100644
index 0000000..5fd36a5
Binary files /dev/null and b/assets/joystix/joystix.ttf differ
diff --git a/assets/joystix/read-this.html b/assets/joystix/read-this.html
new file mode 100644
index 0000000..f5b5020
--- /dev/null
+++ b/assets/joystix/read-this.html
@@ -0,0 +1,195 @@
+
+
+Typodermic Fonts Inc. Freeware Font instructions 2014
+
+
+
+
+
+
+
+
+
+
+
+Deutsch
+
+Español
+
+Français
+
+العربية
+
+中国简体
+
+簡體字
+
+Čeština
+
+Ελληνικά
+
+עִבְרִית
+
+हिन्दी
+
+Magyar
+
+Íslenska
+
+Indonesia
+
+Italiano
+
+한국말
+
+日本語
+
+Norsk
+Język Polski
+Português
+ਪੰਜਾਬੀ
+Română
+Русский
+Svenska
+தமிழ்
+ภาษาไทย
+Türkçe
+Українська мова
+Tiếng Việt
+
+
+
+Thanks for downloading a free font from Typodermic Fonts Inc. This font is free for commercial use. Read the attached license agreement for details.
+
+
+Installation
+
+
+- Windows 8/7/Vista: Get the font out of the ZIP. Right-click on the font and Install
+
- Windows XP: Get the font out of the ZIP. Put it into Start Menu/Control Panel/Appearance and Themes/Fonts
+
- Mac OS X: Get the font out of the ZIP. Put it into /Library/Fonts or /Users/Your_username/Library/Fonts
+
- Mac OS X 10.3 or above: Double-click the font file and Install font
+
+
+
+Allowed
+
+
+- art
+
- sign
+
- poster
+
- banner
+
- book
+
- business card
+
- album
+
- movie
+
- television
+
- logo
+
- trademarked logo
+
- clothing
+
- sticker
+
- stamp
+
- product label
+
- web page (not embedded)
+
- app (not embedded)
+
- PDF (not editable)
+
+
+Not allowed
+
+
+- ebook
+
- app (embedded)
+
- web page (embedded)
+
- product creation platform
+
- alphabet stamps
+
- advertisment server
+
- web template
+
- PDF (editable)
+
- OEM
+
- device embedding
+
+
+It’s easy to get a different license agreement. Read the this page for details.
+
+Other styles
+
+Many of my free fonts have other styles available. Please visit Typodermic Fonts and search for the name of this font in the search bar.
+
+
+About me
+
+My name is Ray Larabie and I’ve been creating fonts since 1996.
+
+Please visit typodermicfonts.com to find out more.
+
+Facebook
+About.me
+Twitter
+Pinterest
+
+
\ No newline at end of file
diff --git a/assets/joystix/typodermic-eula-02-2014.pdf b/assets/joystix/typodermic-eula-02-2014.pdf
new file mode 100644
index 0000000..e5de28e
Binary files /dev/null and b/assets/joystix/typodermic-eula-02-2014.pdf differ
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 8e7547b..3b92e8e 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -1,6 +1,11 @@
find_package(sdl2-image CONFIG REQUIRED)
+find_package(sdl2-ttf CONFIG REQUIRED)
+find_package(fmt CONFIG REQUIRED)
+
+
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "*.cpp")
+
add_library(libpacman ${sources})
target_include_directories(libpacman PUBLIC .)
-target_link_libraries(libpacman PUBLIC SDL2::SDL2-static SDL2::SDL2_image)
+target_link_libraries(libpacman PUBLIC SDL2::SDL2-static SDL2::SDL2_image SDL2::SDL2_ttf fmt::fmt)
diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp
index 2890e1c..2c494b4 100644
--- a/lib/Canvas.cpp
+++ b/lib/Canvas.cpp
@@ -1,7 +1,6 @@
#include "Canvas.hpp"
-
#include
-
+#include
#include "PacMan.hpp"
#include "Pellets.hpp"
#include "SuperPellets.hpp"
@@ -16,6 +15,7 @@ void Canvas::update(const PacMan & pacMan, const Pellets & pellets, const SuperP
renderPellets(pellets);
renderSuperPellets(superPellets);
renderPacMan(pacMan);
+ renderScore(0);
window.render();
}
@@ -48,6 +48,13 @@ void Canvas::renderPacMan(const PacMan & pac_man) const {
renderSprite(pacmanSprite, SDL_Point{ int(pos.x), int(pos.y) });
}
+void Canvas::renderScore(int score) {
+ const int x = LEFT_MARGIN + MAZE_WIDTH + LEFT_MARGIN;
+ const int y = TOP_MARGIN * 2;
+ window.drawText("SCORE", {x, y});
+ window.drawText(fmt::format("{}", score), {x, y + 20});
+}
+
SDL_Rect Canvas::windowDimensions() const {
return { 0, 0, LEFT_MARGIN + MAZE_WIDTH + SCORE_WIDTH, TOP_MARGIN + MAZE_HEIGHT + BOTTOM_MARGIN };
}
diff --git a/lib/Canvas.hpp b/lib/Canvas.hpp
index 3cdac44..fee3d9f 100644
--- a/lib/Canvas.hpp
+++ b/lib/Canvas.hpp
@@ -27,6 +27,7 @@ private:
void renderPellets(const Pellets & pellets) const;
void renderSuperPellets(const SuperPellets & superPellets) const;
void renderSprite(Sprite sprite, SDL_Point point) const;
+ void renderScore(int score);
SDL_Rect windowDimensions() const;
Sprite getSprite(SDL_Point rect) const;
diff --git a/lib/SDLWindow.cpp b/lib/SDLWindow.cpp
index 9d81b1c..c3cbf15 100644
--- a/lib/SDLWindow.cpp
+++ b/lib/SDLWindow.cpp
@@ -6,6 +6,7 @@
SDLWindow::SDLWindow(SDL_Rect windowGeometry) {
initSDL();
initSDLImage();
+ initSDLTTF();
createWindow(windowGeometry.w, windowGeometry.h);
createRenderer();
@@ -24,11 +25,7 @@ void SDLWindow::render() {
}
Sprite SDLWindow::getBackground() const {
- int w, h;
- if (SDL_QueryTexture(maze_texture.get(), nullptr, nullptr, &w, &h) != 0) {
- exitFailure("Failed to get texture geometry");
- }
-
+ auto [w, h] = textureSize(maze_texture.get());
SDL_Rect maze_rect = { 0, 0, w, h };
return { maze_texture.get(), maze_rect };
}
@@ -42,6 +39,15 @@ void SDLWindow::renderSprite(Sprite sprite, SDL_Rect target) const {
exitFailure("Failed to copy texture to renderer");
}
+void SDLWindow::drawText(const std::string & text, SDL_Point position, SDL_Color textColor) const
+{
+ SDLSurfacePtr surface(TTF_RenderUTF8_Solid(font.get(), text.data(), textColor), SDL_Surface_Deleter{});
+ SDLTexturePtr textTexture(SDL_CreateTextureFromSurface(renderer.get(), surface.get()), SDL_Texture_Deleter{});
+ auto [w, h] = textureSize(textTexture.get());
+ SDL_Rect textLocation = { position.x, position.y, w, h };
+ SDL_RenderCopy(renderer.get(), textTexture.get(), NULL, &textLocation);
+}
+
void SDLWindow::initSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
exitFailure("Failed to initialize the SDL2 library");
@@ -53,6 +59,16 @@ void SDLWindow::initSDLImage() {
exitImgFailure("Failed to init SDL_Image with png");
}
+void SDLWindow::initSDLTTF() {
+ if(TTF_Init() != 0) {
+ exitFailure("Unable to setup font library");
+ }
+ font.reset(TTF_OpenFont("joystix.ttf", 20));
+ if(!font) {
+ exitFailure("Failed to copy texture to renderer");
+ }
+}
+
void SDLWindow::createWindow(int width, int height) {
window = std::unique_ptr(SDL_CreateWindow(
"Pacman",
@@ -100,6 +116,15 @@ SDLWindow::loadTexture(const std::string & path) {
return texture;
}
+std::tuple SDLWindow::textureSize(SDL_Texture* texture) const
+{
+ int w, h;
+ if (SDL_QueryTexture(texture, nullptr, nullptr, &w, &h) != 0) {
+ exitFailure("Failed to get texture geometry");
+ }
+ return {w, h};
+}
+
void SDLWindow::exitFailure(const std::string & message) {
std::cerr << message << "\n";
std::cerr << "SDL2 Error: " << SDL_GetError() << "\n";
diff --git a/lib/SDLWindow.hpp b/lib/SDLWindow.hpp
index d0c15e5..8bbcad4 100644
--- a/lib/SDLWindow.hpp
+++ b/lib/SDLWindow.hpp
@@ -2,6 +2,7 @@
#include "Sprite.hpp"
#include
+#include
#include
#include
@@ -29,11 +30,16 @@ struct SDL_Texture_Deleter {
}
};
-struct TextureSize {
- int width;
- int height;
+struct SDL_Font_Deleter {
+ void operator()(TTF_Font* font) {
+ TTF_CloseFont(font);
+ }
};
+using SDLTexturePtr = std::unique_ptr;
+using SDLSurfacePtr = std::unique_ptr;
+
+
class PacMan;
class Pellets;
class SuperPellets;
@@ -42,6 +48,8 @@ class SDLWindow {
public:
explicit SDLWindow(SDL_Rect windowGeometry);
+ static constexpr auto White = SDL_Color{0xFF, 0xFF, 0xFF};
+
void clear();
void render();
@@ -49,15 +57,17 @@ public:
Sprite getBackground() const;
Sprite getSprite(SDL_Rect rect) const;
void renderSprite(Sprite sprite, SDL_Rect target) const;
+ void drawText(const std::string &text, SDL_Point position, SDL_Color textColor = White) const;
private:
static constexpr int16_t SCALE_FACTOR = 1;
std::unique_ptr window;
std::unique_ptr renderer;
- std::unique_ptr window_surface;
- std::unique_ptr maze_texture;
- std::unique_ptr sprite_texture;
+ SDLSurfacePtr window_surface;
+ SDLTexturePtr maze_texture;
+ SDLTexturePtr sprite_texture;
+ std::unique_ptr font;
void createWindow(int width, int height);
@@ -69,6 +79,8 @@ private:
static void initSDLImage();
+ void initSDLTTF();
+
void setDrawColor();
static void exitFailure(const std::string & message);
@@ -79,4 +91,6 @@ private:
loadTexture(const std::string & path);
SDL_Rect windowDimensions() const;
+
+ std::tuple textureSize(SDL_Texture* texture) const;
};
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 15c6b9b..abdf99c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -16,6 +16,7 @@ add_custom_command(TARGET pacman POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/assets/maze.png
${PROJECT_SOURCE_DIR}/assets/sprites32.png
+ ${PROJECT_SOURCE_DIR}/assets/joystix/joystix.ttf
$)
#target_compile_options(pacman PRIVATE -fsanitize=address) # /MD will be used implicitly
diff --git a/vcpkg.json b/vcpkg.json
index 822f05d..0e4cae9 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -4,6 +4,8 @@
"version": "0.1",
"dependencies": [
"sdl2-image",
+ "sdl2-ttf",
+ "fmt",
"gtest"
]
}
\ No newline at end of file