-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
242 lines (194 loc) · 8.27 KB
/
main.cpp
File metadata and controls
242 lines (194 loc) · 8.27 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <string.h>
#include "formulae.cpp"
#include "render.h"
#define HEIGHT 1000
#define WIDTH 1000
#define PI 3.14156
sf::Image image;
int image_size_x = 1;
int image_size_y = 1;
Color colour(255, 126, 100);
sf::Image prev_image;
char last_load[128];
unsigned char colour_threshold = 10;
///Measured in the increment in rads fromn one line to the next.
float draw_density = 0.0002f;
float start_angle = 0;
float end_angle = 2*PI;
float* distance_map;
int load_image(char* filepath){
if (!image.loadFromFile(filepath))
{
return 100;
}
image_size_x = image.getSize().x;
image_size_y = image.getSize().y;
printf("Successfully loaded image with resolution %dx%dpx\n", image_size_x, image_size_y);
strcpy(last_load, filepath);
return 0;
}
float image_scale;
void set_sprite_scale(sf::Sprite* sprite){
float h_ratio = (float)WIDTH/(float)image_size_x;
float w_ratio = (float)HEIGHT/(float)image_size_y;
float ratio = h_ratio < w_ratio ? h_ratio : w_ratio; // picks smaller ratio of the two
image_scale = ratio;
sprite->setScale(sf::Vector2f(ratio, ratio));
}
int main(int argc, char** argv){
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Scanline Computer");
sf::Texture texture;
if (argc == 2){
load_image(argv[1]);
}
int target_x = 0;
int target_y = 0;
sf::CircleShape target(4.0f);
target.setFillColor(colour);
target.setOrigin(sf::Vector2f(2.0f, 2.0f));
distance_map = (float*)malloc(sizeof(float));
while (true){
sf::Texture texture;
if(!texture.loadFromImage(image)){
printf("Load image using `load <filepath>`\n");
};
sf::Sprite sprite;
sprite.setTexture(texture);
set_sprite_scale(&sprite);
window.clear();
window.draw(sprite);
window.draw(target);
window.display();
////////////////////////////////////// PARSE INPUT /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
char input[128]; // parse input
char operation[128];
char operand[128];
scanf(" %126[^\n]", input);
char stage = 0; // stage is 0 until it finds where second part (operand) begins
for(int i = 0; i<128; i++){
if(stage == 0){
if (input[i] == '\00'){
operation[i] = '\00';
operand[0] = '\00';
break;
}else if (input[i] == ' '){
operation[i] = '\00';
stage = i+1;
}else{operation[i] = input[i];}; // copy opearation
}
else{
if (input[i] == '\00' || input[i] == ' '){
operand[i-stage] = '\00';
break;
}else{operand[i-stage] = input[i];}
}
}
if(!strcmp(operation, "exit")){
window.close();
return 0;
}else if(!strcmp(operation, "help")){
printf("\
load <filepath> Loads from given filepath. Accepts bmp, png, tga, jpg, gif, psd, hdr, pic and pnm. Jpg without arithmetic encoding.\n\
reload Reloads from last successfully loaded file, good for resetting.\n\
save <filepath> Saves to given filepath.\n\
saveperspective <filepath> Saves a perspective template to given filepath.\n\
\n\
sx <int> Set target x\n\
sy <int> Set target y\n\
nx <int> Nudge target x\n\
ny <int> Nudge target y\n\
getpos Returns current target pos\n\
density <float> of drawing in scans per degree\n\
\n\
setstart <angle> Sets start angle. In Degrees, as a bearing from North.\n\
setend <angle> Sets end angle. Likewise.\n\
threshold <int> Sets low pass threshold that cancels the line. 0 is unstoppable, 255 is max. Default 10.\n\
\n\
r <int> Sets colour of drawing. Stored out of 255.\n\
g <int> Likewise\n\
b <int> Likewise\n\
getcolour Returns current colour\n\
\n\
draw Draw!\n\
sketch Draw at a low density without having to change programmed density.\n\
\n\
redraw Redraws window.\n\
exit\n\
\n");
}else if(!strcmp(operation, "load")){
load_image(operand);
}else if(!strcmp(operation, "reload")){
if(last_load[0] == '\00'){printf("No last load to load from. Please use load <filepath>\n");}
else{load_image(last_load);}
}else if(!strcmp(operation, "save")){
printf("...");
if( image.saveToFile(operand) ){
printf("Successfully saved image to `%s`.\n", operand);
}else{
printf("Failed to save image.\n");
}
}else if(!strcmp(operation, "saveperspective")){
printf("...");
Image drawing = render::draw(distance_map, abs(end_angle-start_angle));
if( drawing.saveToFile(operand) ){
printf("Successfully saved perspective sketch to `%s`.\n", operand);
}else{
printf("Failed to save Perspective Sketch.\n");
}
}else if(!strcmp(operation, "undo")){
image = prev_image;
}else if(!strcmp(operation, "redraw")){
}else if(!strcmp(operation, "draw")){
prev_image = image; // prepare for undo;
free(distance_map);
distance_map = draw_sight(&image, target_x, target_y, start_angle, end_angle, draw_density, colour, colour_threshold);
}else if(!strcmp(operation, "sketch")){
prev_image = image;
free(distance_map);
distance_map = draw_sight(&image, target_x, target_y, start_angle, end_angle, 0.002, colour, colour_threshold);
}else if(!strcmp(operation, "setstart")){
start_angle = PI / 180.0f * atof(operand);
}else if(!strcmp(operation, "setend")){
end_angle = PI / 180.0f * atof(operand);
}else if(!strcmp(operation, "threshold")){
colour_threshold = atoi(operand);
}else if(!strcmp(operation, "r")){
colour.r = atoi(operand);
target.setFillColor(colour);
//printf("New colour set to rgb %d %d %d.\n", colour.r, colour.g, colour.b);
}else if(!strcmp(operation, "g")){
colour.g = atoi(operand);
target.setFillColor(colour);
//printf("New colour set to rgb %d %d %d.\n", colour.r, colour.g, colour.b);
}else if(!strcmp(operation, "b")){
colour.b = atoi(operand);
target.setFillColor(colour);
//printf("New colour set to rgb %d %d %d.\n", colour.r, colour.g, colour.b);
}else if(!strcmp(operation, "getcolour")){
printf("Colour = %dr %dg %db.\n", colour.r, colour.g, colour.b);
}else if(!strcmp(operation, "sx")){
target_x = atoi(operand);
target.setPosition(image_scale * sf::Vector2f(target_x, target_y));
}else if(!strcmp(operation, "sy")){
target_y = atoi(operand);
target.setPosition(image_scale * sf::Vector2f(target_x, target_y));
}else if(!strcmp(operation, "nx")){
target_x += atoi(operand);
target.setPosition(image_scale * sf::Vector2f(target_x, target_y));
}else if(!strcmp(operation, "ny")){
target_y += atoi(operand);
target.setPosition(image_scale * sf::Vector2f(target_x, target_y));
}else if(!strcmp(operation, "getpos")){
printf("Target position: (%d, %d)\n", target_x, target_y);
}else if(!strcmp(operation, "density")){
draw_density = PI / (180 *float(atof(operand)));
printf("Draw density set to %f scanlinesper degree.\n", atof(operand));
}else{
printf("Invalid command: %s\n", operation);
printf("Write help for a list of commands.\n");
}
}
}