-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
264 lines (240 loc) · 8.81 KB
/
main.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "./libs/include/SDL.h"
#include "./libs/include/SDL_image.h"
#include "./libs/include/SDL_ttf.h"
#include <string.h>
#include "./render.h"
#include "./wrappers/pio-window.h"
#include "./output.h"
#include "./camera.h"
#define INIT_WIDTH 1366
#define INIT_HEIGHT 720
const char *TITLE = "pioEditor";
output_t gOutput;
camera_t gCamera;
pioWindow_t gWindow;
SDL_Renderer *gRenderer = NULL;
// If successfull, return 1
// Else, return 0
int init()
{
int success = 1;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = 0;
}
else
{
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!\n");
}
SDL_Window *tmpWindow = SDL_CreateWindow(TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, INIT_WIDTH, INIT_HEIGHT, SDL_WINDOW_RESIZABLE);
if (tmpWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = 0;
}
else
{
gWindow.window = tmpWindow;
updateWindowDims(&gWindow);
gRenderer = SDL_CreateRenderer(gWindow.window, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = 0;
}
else
{
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);
//Initialize image loading
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = 0;
}
//Initialize font loading
if (TTF_Init() == -1)
{
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
success = 0;
}
}
}
}
return success;
}
// Free everything
void closeAll()
{
closeMedia();
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
destroyPioWindow(&gWindow);
IMG_Quit();
TTF_Quit();
SDL_Quit();
}
int main(int argc, char *args[])
{
int successArguments = 0;
if (argc != 6)
{
printf("You need to pass 5 arguments: \n");
printf("Name, time, diaReq, row, col\n");
}
else
{
gOutput.name = (char *)malloc(100 * sizeof(char));
strcpy(gOutput.name, args[1]);
char *timeStr = args[2];
char *diaReqStr = args[3];
char *rowStr = args[4];
char *colStr = args[5];
gOutput.time = atoi(timeStr);
gOutput.diaReq = atoi(diaReqStr);
gOutput.row = atoi(rowStr);
gOutput.col = atoi(colStr);
debugOutput(gOutput);
initArray(&gOutput);
//debugArray(gOutput);
successArguments = 1;
}
gCamera = createCamera();
if (!init() || successArguments == 0)
{
printf("Failed to initialize!\n");
}
else
{
if (!loadMedia(gRenderer))
{
printf("Failed to load media!\n");
}
else
{
int quit = 0;
char selectedTile = dirtTile;
SDL_Event e;
renderFrame(gWindow, gRenderer, gOutput, gCamera);
while (!quit)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
quit = 1;
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
updateCamera(&gCamera, gCamera.row - 1, gCamera.col);
break;
case SDLK_DOWN:
updateCamera(&gCamera, gCamera.row + 1, gCamera.col);
break;
case SDLK_LEFT:
updateCamera(&gCamera, gCamera.row, gCamera.col - 1);
break;
case SDLK_RIGHT:
updateCamera(&gCamera, gCamera.row, gCamera.col + 1);
break;
default:
break;
}
}
else if (e.type == SDL_WINDOWEVENT)
{
switch (e.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
updateWindowDims(&gWindow);
break;
default:
break;
}
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
int mousePosX, mousePosY;
int tileX, tileY;
SDL_GetMouseState(&mousePosX, &mousePosY);
tileX = mousePosX / TILE_WIDTH;
tileY = mousePosY / TILE_HEIGHT;
tileX += gCamera.col; //col
tileY += gCamera.row; //row
//printf("%d,%d, CAM %d,%d POS %d,%d\n", tileY, tileX, gCamera.row, gCamera.col, mousePosX, mousePosY);
if (mousePosY / TILE_HEIGHT >= (gWindow.height / TILE_HEIGHT) - 1)
{
int selTilePosX = mousePosX / TILE_WIDTH;
printf("%d\n", selTilePosX);
switch (selTilePosX)
{
case 0:
selectedTile = dirtTile;
break;
case 1:
selectedTile = emptyTile;
break;
case 2:
selectedTile = rockTile;
break;
case 3:
selectedTile = diamondTile;
break;
case 4:
selectedTile = doorTile;
break;
case 5:
selectedTile = waterTile;
break;
case 6:
selectedTile = borderTile;
break;
case 7:
selectedTile = playerTile;
break;
case 8:
selectedTile = spiderTile;
break;
case 9:
selectedTile = monsterTile;
break;
}
}
else if (tileX < gOutput.col && tileX >= 0 && tileY >= 0 && tileY < gOutput.row)
{
gOutput.arr[tileY][tileX] = selectedTile;
//debugArray(gOutput);
}
} else if(e.button.button == SDL_BUTTON_RIGHT) {
int mousePosX, mousePosY;
int tileX, tileY;
SDL_GetMouseState(&mousePosX, &mousePosY);
tileX = mousePosX / TILE_WIDTH;
tileY = mousePosY / TILE_HEIGHT;
tileX += gCamera.col; //col
tileY += gCamera.row; //row
if (tileX < gOutput.col && tileX >= 0 && tileY >= 0 && tileY < gOutput.row)
{
printf("del\n");
gOutput.arr[tileY][tileX] = emptyTile;
//debugArray(gOutput);
}
}
}
renderFrame(gWindow, gRenderer, gOutput, gCamera);
}
}
}
}
exportOutput(gOutput, "./createdLevel.txt");
closeAll();
return 0;
}