-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaperwriter.cpp
More file actions
129 lines (120 loc) · 3.66 KB
/
paperwriter.cpp
File metadata and controls
129 lines (120 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "paperwriter.h"
#include "imgui_stdlib.h"
#include <algorithm>
SDL_Texture* texPaper;
extern SDL_Renderer* renderer;
void PaperWriter::Init()
{
surfPaper = CreateEmptyPaper();
}
void PaperWriter::RenderUI()
{
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
//ImGui::ShowDemoWindow();
ImGui::Begin("Properties");
static std::string sText;
ImGui::InputText("text", &sText);
if (ImGui::Button("Add text"))
PaperWriter::AddText(sText);
ImGui::End();
/*********************************************************************************/
ImGui::Begin("Hierarchy");
for (size_t i = 0; i < vTexts.size(); i++)
{
ImGui::PushID(i);
ImGui::InputText("Text", &vTexts[i].text);
ImGui::DragFloat2("Position", (float*)&vTexts[i].pos, 1.f, 0, +FLT_MAX, "%.f");
ImGui::InputFloat("Text scale", &vTexts[i].size, .05f, 1.f, "%.2f");
if (ImGui::Button("Remove"))
vTexts.erase(vTexts.begin() + i--);
ImGui::Separator();
ImGui::PopID();
}
ImGui::End();
/*********************************************************************************/
ImGui::Begin("Controls");
static float fZoom = 1.f;
ImGui::SliderFloat("Zoom", &fZoom, 0.05f, 1.f, "%.2f");
if (ImGui::Button("Export to file"))
SDL_SaveBMP(surfPaper, "paper.bmp");
ImGui::End();
/*********************************************************************************/
ImGui::Begin("Paper",nullptr, ImGuiWindowFlags_HorizontalScrollbar);
ImGui::Image(texPaper, { surfPaper->w * fZoom,surfPaper->h * fZoom });
ImGui::End();
/*********************************************************************************/
}
void PaperWriter::AddText(const std::string& text, ImVec2 pos, float textSize)
{
PaperWriter::vTexts.push_back({ text, pos, textSize });
}
void PaperWriter::AddText(const std::string& text, float x, float y, float textSize)
{
PaperWriter::vTexts.push_back({ text, {x, y}, textSize });
}
SDL_Surface* PaperWriter::CreateSurfaceFromText(const Text& text)
{
//TODO: get max width of line
int rows = std::count(text.text.begin(), text.text.end(), '\n') + 1;
SDL_Surface* surf = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)vecPaperSize.y, int(rows * CHAR_HEIGHT * text.size), 32, NULL, NULL, NULL, NULL);
SDL_FillRect(surf, nullptr, SDL_MapRGBA(surf->format, 255, 255, 255, 255));
int row = 0;
int col = 0;
for (char c : text.text)
{
if (c == '\n')
{
row++;
col = 0;
}
else
{
char path[32];
sprintf(path, "characters\\%c.jpg", c);
SDL_Surface* surfChar = IMG_Load(path);
SDL_Rect rect = { col, row * CHAR_HEIGHT * text.size };
SDL_BlitSurface(surfChar, 0, surf, &rect);
col += surfChar->w * text.size;
SDL_FreeSurface(surfChar);
}
}
return surf;
}
SDL_Surface* PaperWriter::CreateEmptyPaper()
{
//TODO: load from file
using namespace PaperWriter;
SDL_Surface* surf = SDL_CreateRGBSurface(SDL_SWSURFACE, vecPaperSize.x, vecPaperSize.y, 32, NULL, NULL, NULL, NULL);
SDL_FillRect(surf, nullptr, SDL_MapRGBA(surf->format, 255, 255, 255, 255));
return surf;
}
void PaperWriter::RedrawPaper()
{
if (surfPaper)
SDL_FreeSurface(surfPaper);
surfPaper = CreateEmptyPaper();
for (const Text& text : vTexts)
{
auto surfText = CreateSurfaceFromText(text);
SDL_Rect rect = { text.pos.x,text.pos.y,surfText->w,surfText->h };
SDL_BlitSurface(surfText, nullptr, surfPaper, &rect);
SDL_FreeSurface(surfText);
}
}
void PaperWriter::DetectChanges()
{
bool update = false;
for (Text& text : vTexts)
{
if (!(text == text.lastState)) //TODO: operator overload
update = true;
text.lastState = std::make_tuple( text.text,text.pos,text.size);
}
if (update)
{
RedrawPaper();
if (texPaper)
SDL_DestroyTexture(texPaper);
texPaper = SDL_CreateTextureFromSurface(renderer, surfPaper);
}
}