Skip to content

Commit f128edc

Browse files
committed
multiline text w/ new text sys
1 parent f48c558 commit f128edc

File tree

4 files changed

+101
-56
lines changed

4 files changed

+101
-56
lines changed

Qor/BasicState.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <cstdlib>
1010
#include <chrono>
1111
#include <thread>
12-
//#include <OALWrapper/OAL_Funcs.h>
1312
using namespace std;
1413
using namespace glm;
1514

@@ -18,10 +17,22 @@ BasicState :: BasicState(Qor* engine):
1817
m_pInput(engine->input()),
1918
m_pRoot(make_shared<Node>()),
2019
m_pPipeline(engine->pipeline()),
21-
m_pResources(engine->resources())
22-
//m_pCanvas(make_shared<Canvas>(
23-
// engine->window()->size().x, engine->window()->size().y
24-
//)),
20+
m_pResources(engine->resources()),
21+
m_pCanvas(make_shared<Canvas>(
22+
engine->window()->size().x, engine->window()->size().y
23+
)),
24+
m_pMenuGUI(make_shared<MenuGUI>(
25+
engine->session()->profile(0)->controller().get(),
26+
&m_MenuContext,
27+
&m_MainMenu,
28+
m_pPipeline->partitioner(),
29+
m_pCanvas.get(),
30+
m_pResources,
31+
"Good Times",
32+
engine->window()->size().y / 24.0f,
33+
nullptr,
34+
5
35+
))
2536
//m_pMenuGUI(make_shared<MenuGUI>(
2637
// engine->session()->active_profile(0)->controller().get(),
2738
// &m_MenuContext,
@@ -48,7 +59,7 @@ void BasicState :: preload()
4859

4960
//m_pFont = m_pQor->resources()->cache_as<Font>("PressStart2P-Regular.ttf:30");
5061
//m_pText = make_shared<Text>(m_pFont);
51-
//m_pText->set("Hello World!");
62+
//m_pText->set("Hello World!\nAgain down here!\nHELLO?\nOK\nOK2\nReally long text here ok?");
5263
//auto text2 = make_shared<Text>(m_pFont);
5364
//text2->set("Testing");
5465
//m_pRoot->add(text2);
@@ -67,14 +78,14 @@ void BasicState :: enter()
6778
m_pCamera->ortho();
6879
m_pPipeline->winding(true);
6980

70-
//m_MainMenu.options().emplace_back("OPTION 1", []{
71-
//});
72-
//m_MainMenu.options().emplace_back("OPTION 2", []{
73-
//});
74-
//m_MainMenu.options().emplace_back("OPTION 3", []{
75-
//});
76-
//m_MenuContext.clear(&m_MainMenu);
77-
//m_pRoot->add(m_pMenuGUI);
81+
m_MainMenu.options().emplace_back("OPTION 1", []{
82+
});
83+
m_MainMenu.options().emplace_back("OPTION 2", []{
84+
});
85+
m_MainMenu.options().emplace_back("OPTION 3", []{
86+
});
87+
m_MenuContext.clear(&m_MainMenu);
88+
m_pRoot->add(m_pMenuGUI);
7889

7990
//LOG("enter");
8091
//m_pRoot->add(Mesh::line(
@@ -88,17 +99,17 @@ void BasicState :: logic(Freq::Time t)
8899
if(m_pInput->key(SDLK_ESCAPE))
89100
m_pQor->quit();
90101

91-
float speed = 1000.0f * t.s();
102+
//float speed = 1000.0f * t.s();
92103

93-
if(m_pInput->key(SDLK_UP))
94-
m_pCamera->move(glm::vec3(0.0f, -speed, 0.0f));
95-
if(m_pInput->key(SDLK_DOWN))
96-
m_pCamera->move(glm::vec3(0.0f, speed, 0.0f));
104+
//if(m_pInput->key(SDLK_UP))
105+
// m_pCamera->move(glm::vec3(0.0f, -speed, 0.0f));
106+
//if(m_pInput->key(SDLK_DOWN))
107+
// m_pCamera->move(glm::vec3(0.0f, speed, 0.0f));
97108

98-
if(m_pInput->key(SDLK_LEFT))
99-
m_pCamera->move(glm::vec3(-speed, 0.0f, 0.0f));
100-
if(m_pInput->key(SDLK_RIGHT))
101-
m_pCamera->move(glm::vec3(speed, 0.0f, 0.0f));
109+
//if(m_pInput->key(SDLK_LEFT))
110+
// m_pCamera->move(glm::vec3(-speed, 0.0f, 0.0f));
111+
//if(m_pInput->key(SDLK_RIGHT))
112+
// m_pCamera->move(glm::vec3(speed, 0.0f, 0.0f));
102113

103114
m_pRoot->logic(t);
104115
}

Qor/BasicState.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class BasicState:
5050
//std::shared_ptr<Font> m_pFont;
5151
//std::shared_ptr<Text> m_pText;
5252

53-
//std::shared_ptr<Canvas> m_pCanvas;
54-
//MenuContext m_MenuContext;
55-
//Menu m_MainMenu;
56-
//std::shared_ptr<MenuGUI> m_pMenuGUI;
53+
std::shared_ptr<Canvas> m_pCanvas;
54+
MenuContext m_MenuContext;
55+
Menu m_MainMenu;
56+
std::shared_ptr<MenuGUI> m_pMenuGUI;
5757
};
5858

5959
#endif

Qor/Text.cpp

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <boost/algorithm/string.hpp>
12
#include "Text.h"
23
#include <boost/lexical_cast.hpp>
34
#include "kit/log/log.h"
@@ -16,8 +17,8 @@ Font :: Font(const std::string& fn, ICache* c)
1617
if(split_point == string::npos)
1718
split_point = fn.size();
1819
else
19-
size = boost::lexical_cast<int>(fn.substr(split_point+1));
20-
m_pFont = TTF_OpenFont((fn.substr(0,split_point)).c_str(), size);
20+
m_Size = boost::lexical_cast<int>(fn.substr(split_point+1));
21+
m_pFont = TTF_OpenFont((fn.substr(0,split_point)).c_str(), m_Size);
2122
if(!m_pFont){
2223
K_ERRORf(READ, "font \"%s\"", fn);
2324
}
@@ -70,28 +71,61 @@ Text :: Text(const std::shared_ptr<Font>& font):
7071

7172
Text :: ~Text()
7273
{
73-
if(m_pSurface){
74-
SDL_FreeSurface(m_pSurface);
75-
m_pSurface = nullptr;
76-
}
74+
//if(m_pSurface){
75+
// SDL_FreeSurface(m_pSurface);
76+
// m_pSurface = nullptr;
77+
//}
7778

7879
deinit();
7980
}
8081

8182
void Text :: redraw()
8283
{
83-
if(m_pSurface){
84-
SDL_FreeSurface(m_pSurface);
85-
m_pSurface = nullptr;
86-
}
84+
//if(m_pSurface){
85+
// SDL_FreeSurface(m_pSurface);
86+
// m_pSurface = nullptr;
87+
//}
8788

8889
SDL_Color color;
8990
color.r = Uint8(m_Color.r() * 0xFF);
9091
color.g = Uint8(m_Color.g() * 0xFF);
9192
color.b = Uint8(m_Color.b() * 0xFF);
9293
color.a = Uint8(m_Color.a() * 0xFF);
93-
m_pSurface = TTF_RenderText_Solid(m_pFont->font(),m_Text.c_str(),color);
94-
assert(m_pSurface);
94+
95+
vector<string> lines;
96+
boost::split(lines, m_Text, boost::is_any_of("\n"));
97+
98+
SDL_Surface* tmp = nullptr;
99+
SDL_Rect rect;
100+
101+
int width=0;
102+
int lineheight=0;
103+
int height=0;
104+
for(int i=0;i<lines.size();++i){
105+
int sz;
106+
TTF_SizeText(m_pFont->font(), lines[i].c_str(), &sz, &lineheight);
107+
if(sz > width)
108+
width = sz;
109+
}
110+
111+
for(int i=0;i<lines.size();++i){
112+
auto surf = TTF_RenderText_Solid(m_pFont->font(), lines[i].c_str(), color);
113+
if(i == 0){
114+
height = (lineheight + m_pFont->size()*m_LineSpacing) * lines.size();
115+
tmp = SDL_CreateRGBSurface(0, width, height,
116+
32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
117+
rect.x = 0;
118+
rect.w = width;
119+
rect.h = height;
120+
cout << width << endl;
121+
cout << height << endl;
122+
}
123+
rect.y = i * (lineheight + m_LineSpacing*m_pFont->size());
124+
assert(surf);
125+
SDL_BlitSurface(surf, NULL, tmp, &rect);
126+
SDL_FreeSurface(surf);
127+
}
128+
assert(tmp);
95129

96130
m_pTexture = nullptr;
97131

@@ -100,34 +134,30 @@ void Text :: redraw()
100134
GL_TASK_START()
101135
glGenTextures(1, &m_ID);
102136
glBindTexture(GL_TEXTURE_2D, m_ID);
103-
int mode = GL_RGB;
104-
//if(m_pSurface->format->BytesPerPixel == 4)
105-
mode = GL_RGBA;
137+
int mode = GL_RGBA;
106138

107-
SDL_Surface* tmp = SDL_CreateRGBSurface(0,m_pSurface->w, m_pSurface->h,
108-
32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
109-
SDL_BlitSurface(m_pSurface, NULL, tmp, NULL);
110139
glTexImage2D(GL_TEXTURE_2D, 0, mode, tmp->w, tmp->h,
111140
0, mode, GL_UNSIGNED_BYTE, tmp->pixels);
112141

113142
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114143
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
115-
SDL_FreeSurface(tmp);
116144
GL_TASK_END()
117145

146+
SDL_FreeSurface(tmp);
147+
118148
m_pTexture = make_shared<Texture>(m_ID);
119149

120150
glm::vec2 vs,ve;
121-
if(m_Align == LEFT){
151+
//if(m_Align == LEFT){
122152
vs = vec2(0.0f, 0.0f);
123-
ve = vec2(m_pSurface->w, m_pSurface->h);
124-
}else if(m_Align == RIGHT){
125-
vs = vec2(-m_pSurface->w, -m_pSurface->h);
126-
ve = vec2(0.0f, 0.0f);
127-
}else{
128-
vs = vec2(-m_pSurface->w / 2.0f, -m_pSurface->h / 2.0f);
129-
ve = vec2(m_pSurface->w / 2.0f, m_pSurface->h / 2.0f);
130-
}
153+
ve = vec2(width, height);
154+
//}else if(m_Align == RIGHT){
155+
// vs = vec2(-rect.x, 0.0f);
156+
// ve = vec2(0.0f, rect.y);
157+
//}else{
158+
// vs = vec2(-rect.x / 2.0f, 0.0f);
159+
// ve = vec2(rect.x / 2.0f, rect.y);
160+
//}
131161
m_pMesh = make_shared<Mesh>(
132162
make_shared<MeshGeometry>(Prefab::quad(vs, ve)),
133163
vector<shared_ptr<IMeshModifier>>{

Qor/Text.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ class Font:
2626

2727
TTF_Font* font() { return m_pFont; }
2828

29+
int size() const { return m_Size; }
30+
2931
private:
3032

3133
TTF_Font* m_pFont = nullptr;
3234
glm::vec2 m_WindowSize;
35+
int m_Size;
3336
};
3437

3538
class Text:
@@ -72,7 +75,8 @@ class Text:
7275
Color m_Color = Color::white();
7376
Align m_Align = LEFT;
7477
glm::vec2 m_Pos;
75-
SDL_Surface* m_pSurface = nullptr;
78+
//SDL_Surface* m_pSurface = nullptr;
79+
float m_LineSpacing = 0.0f;
7680

7781
std::shared_ptr<Texture> m_pTexture;
7882
std::shared_ptr<Mesh> m_pMesh;

0 commit comments

Comments
 (0)