about summary refs log tree commit diff stats
path: root/lunatic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lunatic.cpp')
-rw-r--r--lunatic.cpp120
1 files changed, 105 insertions, 15 deletions
diff --git a/lunatic.cpp b/lunatic.cpp index 497c648..291bd09 100644 --- a/lunatic.cpp +++ b/lunatic.cpp
@@ -6,8 +6,34 @@
6#include <chrono> 6#include <chrono>
7#include <thread> 7#include <thread>
8#include <Magick++.h> 8#include <Magick++.h>
9#include <list>
10#include <string>
11#include <sstream>
9#include "database.h" 12#include "database.h"
10 13
14template <class Container>
15Container split(std::string input, std::string delimiter)
16{
17 Container result;
18
19 while (!input.empty())
20 {
21 int divider = input.find(delimiter);
22 if (divider == std::string::npos)
23 {
24 result.push_back(input);
25
26 input = "";
27 } else {
28 result.push_back(input.substr(0, divider));
29
30 input = input.substr(divider+delimiter.length());
31 }
32 }
33
34 return result;
35}
36
11int main(int argc, char** argv) 37int main(int argc, char** argv)
12{ 38{
13 if (argc != 2) 39 if (argc != 2)
@@ -43,15 +69,88 @@ int main(int argc, char** argv)
43 std::string imagePath = config["images"].as<std::string>() 69 std::string imagePath = config["images"].as<std::string>()
44 + "/" + imageName; 70 + "/" + imageName;
45 71
46 Magick::Image overlay;
47 overlay.read("res/overlay.png");
48
49 Magick::Image moonColor; 72 Magick::Image moonColor;
50 moonColor.read("res/" + ach.color + ".png"); 73 moonColor.read("res/" + ach.color + ".png");
51 74
52 try 75 try
53 { 76 {
54 // Start with the game image 77 // Start with the Odyssey text overlay
78 Magick::Image overlay;
79 overlay.read("res/overlay.png");
80
81 // Add the moon image
82 overlay.composite(moonColor, 672, 85, Magick::OverCompositeOp);
83
84 // Add the name of the achievement
85 overlay.fontPointsize(54);
86 overlay.fillColor("white");
87 overlay.font("@" + config["title_font"].as<std::string>());
88
89 std::list<std::string> words = split<std::list<std::string>>(
90 ach.title,
91 " ");
92 std::ostringstream wrappingStream;
93 std::string curline;
94 int lines = 1;
95
96 Magick::TypeMetric metric;
97 while (!words.empty())
98 {
99 std::string temp = curline;
100
101 if (!curline.empty())
102 {
103 temp += " ";
104 }
105
106 temp += words.front();
107
108 overlay.fontTypeMetrics(temp, &metric);
109
110 if (metric.textWidth() > 1200)
111 {
112 wrappingStream << std::endl;
113 curline = words.front();
114
115 lines++;
116 } else {
117 if (!curline.empty())
118 {
119 wrappingStream << " ";
120 }
121
122 curline = temp;
123 }
124
125 wrappingStream << words.front();
126 words.pop_front();
127 }
128
129 std::string wrapped = wrappingStream.str();
130
131 overlay.annotate(
132 wrapped,
133 Magick::Geometry(1600, 228, 0, 710),
134 Magick::GravityType::NorthGravity);
135
136 // Add the achievement date
137 did theDid = db.getRandomDidForAchievement(ach.achievementId);
138
139 overlay.fontTypeMetrics(wrapped, &metric);
140
141 overlay.fontPointsize(20);
142 overlay.font("@" + config["date_font"].as<std::string>());
143 overlay.annotate(
144 theDid.date,
145 Magick::Geometry(1600, 228, 0, 710 + metric.textHeight() * lines - 22),
146 Magick::GravityType::NorthGravity);
147
148 // Make a shadow copy
149 Magick::Image shadow(overlay);
150 shadow.negate();
151 shadow.blur(0, 12);
152
153 // Read the game image
55 Magick::Image image; 154 Magick::Image image;
56 image.read(imagePath); 155 image.read(imagePath);
57 156
@@ -60,18 +159,9 @@ int main(int argc, char** argv)
60 image.scale("80x45"); 159 image.scale("80x45");
61 image.scale("1600x900"); 160 image.scale("1600x900");
62 161
63 // Add the text and moon image from Odyssey 162 // Add the generated overlay to it
163 image.composite(shadow, 0, 0, Magick::OverCompositeOp);
64 image.composite(overlay, 0, 0, Magick::OverCompositeOp); 164 image.composite(overlay, 0, 0, Magick::OverCompositeOp);
65 image.composite(moonColor, 672, 85, Magick::OverCompositeOp);
66
67 // Add the name of the achievement
68 image.fontPointsize(36);
69 image.fillColor("white");
70 image.font("@" + config["font"].as<std::string>());
71 image.annotate(
72 ach.title,
73 Magick::Geometry(0, 0, 0, 672),
74 Magick::GravityType::NorthGravity);
75 165
76 // Output for debug 166 // Output for debug
77 image.magick("png"); 167 image.magick("png");