Skip to content

Commit 0146bb3

Browse files
committed
Merge pull request #21 from Databean/addSDL_ttf
Add sdl ttf
2 parents 8b764d6 + cfb0ff1 commit 0146bb3

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ALLFILES := $(wildcard $(SRC_HOME)/*) $(wildcard $(INCL_HOME)/*)
88
export CXX := g++
99
export LD := g++
1010
export CXXFLAGS := -g -I$(INCL_HOME) -std=c++0x -Wall
11-
export LDFLAGS := -L/usr/local/lib -lSDL2 -lGL -lGLU
11+
export LDFLAGS := -L/usr/local/lib -lSDL2 -lSDL2_ttf -lGL -lGLU
1212

1313
.PHONY: all
1414
all: $(EXECUTABLE)

include/Renderer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef RENDERER_H
22
#define RENDERER_H
33

4+
#include <string>
5+
46
#include <SDL2/SDL.h>
57
#include <SDL2/SDL_opengl.h>
68
#include <GL/gl.h>
@@ -12,6 +14,8 @@ void renderBoard(const GameBoard& board, const Player& perspective);
1214

1315
GLuint loadImageAsTexture(const std::string& name);
1416

17+
void renderText(const std::string& font, int fontSize, const std::pair<float, float> bottomLeft, const std::pair<float, float> topRight, const std::string& text);
18+
1519
std::pair<float, float> coordToScreen(const Coordinate& coord);
1620
Coordinate screenToCoord(const std::pair<float, float>&);
1721

49.5 KB
Binary file not shown.
44.5 KB
Binary file not shown.

src/GameView.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ void GameView::render() {
9393
it->render();
9494
}
9595

96+
renderText("resources/TypeWritersSubstitute-Black.ttf", 20, {.2, .9}, {.8, 1}, "Settlers of Catan");
97+
9698
glFlush();
9799
}
98100

src/Renderer.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <SDL2/SDL.h>
1313
#include <SDL2/SDL_opengl.h>
14+
#include <SDL2/SDL_ttf.h>
1415
#include <GL/gl.h>
1516

1617
#include "GameBoard.h"
@@ -21,6 +22,59 @@ using std::pair;
2122
using std::runtime_error;
2223
using std::string;
2324

25+
/**
26+
* Render some text on screen.
27+
*
28+
* @param fontPath The path to the font ttf
29+
* @param fontSize The size to draw the letters in.
30+
* @param bottomLeft The bottom left screen coordinate of the bounding box to draw to.
31+
* @param topRight The top right screen coordinate of the bounding box to draw to.
32+
* @param text The text to render.
33+
*/
34+
void renderText(const std::string& fontPath, int fontSize, const std::pair<float, float> bottomLeft, const std::pair<float, float> topRight, const std::string& text) {
35+
TTF_Font* font = TTF_OpenFont(fontPath.c_str(), fontSize);
36+
if(!font) {
37+
throw runtime_error("TTF_OpenFont: " + string(TTF_GetError()));
38+
}
39+
40+
SDL_Color color { 0, 0, 0, 255 };
41+
42+
SDL_Surface* textSurface = TTF_RenderText_Solid(font, text.c_str(), color);
43+
TTF_CloseFont(font);
44+
45+
if(!textSurface) {
46+
throw runtime_error("TTF_RenderText_Solid: " + string(TTF_GetError()));
47+
}
48+
49+
SDL_PixelFormat* format = SDL_AllocFormat(SDL_PIXELFORMAT_RGB888);
50+
SDL_Surface* imageSurface = SDL_ConvertSurface(textSurface, format, 0);
51+
SDL_FreeSurface(textSurface);
52+
SDL_FreeFormat(format);
53+
54+
GLuint texture;
55+
glGenTextures(1, &texture);
56+
glBindTexture(GL_TEXTURE_2D, texture);
57+
58+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageSurface->w, imageSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageSurface->pixels);
59+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
60+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
61+
62+
SDL_FreeSurface(imageSurface);
63+
64+
glBegin(GL_QUADS);
65+
glTexCoord2f(0, 1);
66+
glVertex2f(bottomLeft.first, bottomLeft.second);
67+
glTexCoord2f(0, 0);
68+
glVertex2f(bottomLeft.first, topRight.second);
69+
glTexCoord2f(1, 0);
70+
glVertex2f(topRight.first, topRight.second);
71+
glTexCoord2f(1, 1);
72+
glVertex2f(topRight.first, bottomLeft.second);
73+
glEnd();
74+
75+
glDeleteTextures(1, &texture);
76+
}
77+
2478
/**
2579
* Loads an image into an OpenGL texture.
2680
* @param name The file name of the texture to load.

src/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <SDL2/SDL.h>
55
#include <SDL2/SDL_opengl.h>
6+
#include <SDL2/SDL_ttf.h>
67

78
#include <GL/gl.h>
89
#include <GL/glu.h>
@@ -49,11 +50,18 @@ void updateViewport(int width, int height) {
4950
* Main. Initializes SDL and the model, view, and controller. Also has the main game loop.
5051
*/
5152
int main(int argc, char *argv[]) {
53+
const int windowWidth = 900, windowHeight = 800;
54+
55+
if(TTF_Init()==-1) {
56+
std::cout << "Error in TTF_Init: " << TTF_GetError() << std::endl;
57+
return 2;
58+
}
59+
5260
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
5361
SDL_Window* displayWindow;
5462
SDL_Renderer* displayRenderer;
5563
SDL_RendererInfo displayRendererInfo;
56-
SDL_CreateWindowAndRenderer(900, 800, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
64+
SDL_CreateWindowAndRenderer(windowWidth, windowHeight, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
5765
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
5866
/*TODO: Check that we have OpenGL */
5967
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||
@@ -66,7 +74,7 @@ int main(int argc, char *argv[]) {
6674

6775
initOpenGL();
6876

69-
updateViewport(1024, 768);
77+
updateViewport(windowWidth, windowHeight);
7078

7179
vector<unique_ptr<Player>> players;
7280
players.emplace_back(unique_ptr<Player>(new Player("test")));

0 commit comments

Comments
 (0)