-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.cpp
More file actions
360 lines (309 loc) · 10.3 KB
/
Editor.cpp
File metadata and controls
360 lines (309 loc) · 10.3 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*******************************************************************************
CommanderTux
Penguin In Space
Released under the GNU Public License
2005 by André Schnabel (thefrogs@web.de)
*******************************************************************************/
// Editor.cpp
#include "Editor.h"
//
// CEditor
// Constructor
//
CEditor::CEditor() : CState()
{
DEBUG( "Editor is starting... " );
// Video stuff
g_framework->ShowCursor( true );
g_framework->SetVideoMode( EDIT_W, EDIT_H );
// Allocate new level from scratch
m_level = NULL;
m_level = new CLevel( DATA_PREFIX "/levels/level3.ctux", true, false );
//m_level = new CLevel( NULL, true, false );
// Tile box
m_tile_box.selected = 0;
m_tile_box.ypos = EDIT_H - ( TILE_H + 10 ) - 110;
// Enemy box
m_enemy_box.selected = 0;
m_enemy_box.ypos = EDIT_H - ( TILE_H + 10 ) - 70;
// Item box
m_item_box.selected = 0;
m_item_box.ypos = EDIT_H - ( TILE_H + 10 ) - 30;
// Selection surface
m_selection_surf =
g_framework->LoadImage( editor_filenames[ ED_TILE_SELECTED ] );
g_framework->SetColorKey( m_selection_surf, COLOR_MAGENTA );
// Scroll vars
m_scroll_x = 0;
m_scroll_y = (LVL_H_MAX*TILE_H)-(20*TILE_H);
// Selected mode
m_tile_mode = g_framework->LoadImage( mode_filenames[MODE_TILE] );
m_enemy_mode = g_framework->LoadImage( mode_filenames[MODE_ENEMY] );
m_item_mode = g_framework->LoadImage( mode_filenames[MODE_ITEM] );
m_mode = MODE_TILE;
DEBUG( "Editor started!" );
}
//
// CEditor
// Destructor
//
CEditor::~CEditor()
{
DEBUG( "Shutting down the editor... ");
// Free surfaces
SDL_DestroyTexture( m_item_mode );
SDL_DestroyTexture( m_enemy_mode );
SDL_DestroyTexture( m_tile_mode );
SDL_DestroyTexture( m_selection_surf );
// Delete the level
delete m_level;
// Reset to old video mode
g_framework->SetVideoMode();
// Set cursor deactive for other modes
g_framework->ShowCursor( false );
DEBUG( "Editor shut down!");
}
//
// Input
// Editor specific input.
//
void CEditor::Input()
{
static SDL_Rect temp_rect, temp_rect2;
// Standard input
CState::Input();
if( m_keys[SDL_SCANCODE_ESCAPE] )
m_done = true;
// MOUSE INPUT
if( SDL_GetMouseState( &m_mouse_x, &m_mouse_y ) & SDL_BUTTON( 1 ) )
{
// For coll detection
temp_rect2.w = 10; temp_rect2.h = 10;
temp_rect2.x = m_mouse_x;
temp_rect2.y = m_mouse_y;
temp_rect.w = TILE_W; temp_rect.h = TILE_H;
// Click into the grid
if( m_mouse_x < 900 && m_mouse_x > 0 &&
m_mouse_y < 600 && m_mouse_y > 0 )
{
if( m_mode == MODE_TILE ) // Tile mode
{
if( m_tile_box.selected < NUM_TILES )
{
m_level->GetLevelStruct()->content
[(m_mouse_y+m_scroll_y)/TILE_H][(m_mouse_x+m_scroll_x)/TILE_W]
= m_tile_box.selected;
}
else
m_level->GetLevelStruct()->content[(m_mouse_y+m_scroll_y)/
TILE_H][(m_mouse_x+m_scroll_x)/TILE_W] = TILE_EMPTY;
}
else if( m_mode == MODE_ENEMY ) // Enemy mode
{
if( m_enemy_box.selected < NUM_ENEMIES )
{
m_level->GetLevelStruct()->content
[(m_mouse_y+m_scroll_y)/TILE_H][(m_mouse_x+m_scroll_x)/TILE_W]
= m_enemy_box.selected+ENEMY_SHIFT;
}
else
m_level->GetLevelStruct()->content[(m_mouse_y+m_scroll_y)/
TILE_H][(m_mouse_x+m_scroll_x)/TILE_W] = TILE_EMPTY;
}
else // Item mode
{
if( m_item_box.selected < NUM_ITEMS )
{
m_level->GetLevelStruct()->content
[(m_mouse_y+m_scroll_y)/TILE_H][(m_mouse_x+m_scroll_x)/TILE_W]
= m_item_box.selected+ITEM_SHIFT;
}
else
m_level->GetLevelStruct()->content[(m_mouse_y+m_scroll_y)/
TILE_H][(m_mouse_x+m_scroll_x)/TILE_W] = TILE_EMPTY;
}
}
// Outside the grid
else if( m_mouse_x > 0 && m_mouse_x < EDIT_W &&
m_mouse_y > m_tile_box.ypos &&
m_mouse_y < m_tile_box.ypos + TILE_H ) // Tile box
{
m_tile_box.selected = m_mouse_x / TILE_W;
}
else if( m_mouse_x > 0 && m_mouse_x < EDIT_W &&
m_mouse_y > m_enemy_box.ypos &&
m_mouse_y < m_enemy_box.ypos + TILE_H ) // Enemy box
{
m_enemy_box.selected = m_mouse_x / TILE_W;
}
else if( m_mouse_x > 0 && m_mouse_x < EDIT_W &&
m_mouse_y > m_item_box.ypos &&
m_mouse_y < m_item_box.ypos + TILE_H ) // Item box
{
m_item_box.selected = m_mouse_x / TILE_W;
}
else // Mode selection
{
temp_rect.x = EDIT_W - 40;
temp_rect.y = EDIT_H - 40;
// Tile mode
if( g_framework->Coll( &temp_rect, &temp_rect2 ) )
{
m_mode = MODE_TILE;
}
// Enemy mode
temp_rect.x = EDIT_W - 80;
if( g_framework->Coll( &temp_rect, &temp_rect2 ) )
{
m_mode = MODE_ENEMY;
}
// Item mode
temp_rect.x = EDIT_W - 120;
if( g_framework->Coll( &temp_rect, &temp_rect2 ) )
{
m_mode = MODE_ITEM;
}
}
}
// SCROLLING
if( m_keys[SDL_SCANCODE_RIGHT] )
{
if( m_released.key_right )
{
if( m_scroll_x/TILE_W + 30 < LVL_W_MAX )
{
m_scroll_x+=TILE_W;
m_released.key_right = false;
}
}
}
else
m_released.key_right = true;
if( m_keys[SDL_SCANCODE_LEFT] )
{
if( m_released.key_left )
{
if( m_scroll_x-TILE_W >= 0 )
{
m_scroll_x-=TILE_W;
m_released.key_left = false;
}
}
}
else
m_released.key_left = true;
if( m_keys[SDL_SCANCODE_DOWN] )
{
if( m_released.key_down )
{
if( m_scroll_y/TILE_H + 20 < LVL_H_MAX )
{
m_scroll_y+=TILE_H;
m_released.key_down = false;
}
}
}
else
m_released.key_down = true;
if( m_keys[SDL_SCANCODE_UP] )
{
if( m_released.key_up )
{
if( m_scroll_y-TILE_H >= 0 )
{
m_scroll_y-=TILE_H;
m_released.key_up = false;
}
}
}
else
m_released.key_up = true;
// Special features
if( m_keys[SDL_SCANCODE_S] )
{
if( m_released.key_s )
{
m_level->Save( DATA_PREFIX "/levels/unnamed.ctux" );
m_released.key_s = false;
}
}
else
m_released.key_s = true;
if( m_keys[SDL_SCANCODE_P] )
{
if( m_released.key_p )
{
m_level->PrintLevelContent();
m_released.key_p = false;
}
}
else
m_released.key_p = true;
}
//
// Update
// Editor specific update.
//
void CEditor::Update()
{
// Empty
}
//
// Draw
// Editor specific draw func.
//
void CEditor::Draw()
{
int i;
SDL_RenderClear(g_framework->GetRenderer());
// Clear
g_framework->FillRect( g_framework->GetScreen(), NULL, COLOR_BLACK );
// Lvl
m_level->Draw( &m_scroll_x, &m_scroll_y, 900-TILE_W, 600-TILE_H, true );
// Grid
g_framework->DrawGrid( 30, 30, 900, 600 );
// Tile selection box
for( i=0; i<NUM_TILES; i++ )
g_framework->Draw( m_level->GetTile( i ), i*TILE_W, m_tile_box.ypos );
g_framework->Draw( m_selection_surf, m_tile_box.selected*TILE_W, m_tile_box.ypos );
// Enemy selection box
for( i=0; i<NUM_ENEMIES; i++ )
g_framework->Draw( m_level->GetEnemySurf( i ), i*TILE_W, m_enemy_box.ypos );
g_framework->Draw( m_selection_surf, m_enemy_box.selected*TILE_W, m_enemy_box.ypos );
// Item selection box
for( i=0; i<NUM_ITEMS; i++ )
g_framework->Draw( m_level->GetItemSurf( i ), i*TILE_W, m_item_box.ypos );
g_framework->Draw( m_selection_surf, m_item_box.selected*TILE_W, m_item_box.ypos );
// Position
g_framework->DrawText( "scroll_x/30=", EDIT_W - 100, EDIT_H - 100,
COLOR_YELLOW, F_SIZE_MEDIUM );
g_framework->DrawNumber( m_scroll_x/30, EDIT_W - 20, EDIT_H - 100,
COLOR_YELLOW, F_SIZE_MEDIUM );
g_framework->DrawText( "scroll_y/30=", EDIT_W - 100, EDIT_H - 80,
COLOR_YELLOW, F_SIZE_MEDIUM );
g_framework->DrawNumber( m_scroll_y/30, EDIT_W - 20, EDIT_H - 80,
COLOR_YELLOW, F_SIZE_MEDIUM );
// Help
g_framework->DrawText( "Press 's' to save to 'levels/unnamed.ctux'!",
EDIT_W - 350, EDIT_H - 100,
COLOR_YELLOW, F_SIZE_MEDIUM );
g_framework->DrawText( "Press 'p' to print the level into the stdout!",
EDIT_W - 350, EDIT_H - 80,
COLOR_YELLOW, F_SIZE_MEDIUM );
// Mode
g_framework->Draw( m_tile_mode, EDIT_W - 40, EDIT_H - 40 );
g_framework->Draw( m_enemy_mode, EDIT_W - 80, EDIT_H - 40 );
g_framework->Draw( m_item_mode, EDIT_W - 120, EDIT_H - 40 );
switch( m_mode )
{
case MODE_TILE:
g_framework->Draw( m_selection_surf, EDIT_W - 40, EDIT_H - 40 );
break;
case MODE_ENEMY:
g_framework->Draw( m_selection_surf, EDIT_W - 80, EDIT_H - 40 );
break;
case MODE_ITEM:
g_framework->Draw( m_selection_surf, EDIT_W - 120, EDIT_H - 40 );
break;
}
}