Merge branch 'list' into stage
[cantaveria.git] / editor.c
blobce750bc07c6d3f0a51d29baf2d39db8f47389041
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
23 #include <stdio.h>
25 #include "util.h"
26 #include "backend.h"
27 #include "graphics.h"
28 #include "game.h"
30 #include "loader.h"
32 #include "text.h"
36 int cursor_x;
37 int cursor_y;
38 sprite* cursor;
40 int camera_x;
41 int camera_y;
43 int screen_x;
44 int screen_y;
46 struct {
47 int flag;
48 int key;
49 int timer;
50 } repeat;
52 int hold_fire;
53 unsigned char brush_tile;
57 void press_fire(){
58 //if screen exists change tile
59 //else create new screen
60 zone* z = game.current_zone;
61 int si = game.si;
62 int sj = game.sj;
63 struct screen* scr = ZONE_LOOKUP(z,si,sj);
65 if(!scr){
66 scr = xmalloc(sizeof(struct screen));
67 z->screens[si+z->w*sj] = scr;
70 scr->tiles[cursor_x-si*20][cursor_y-sj*15] = brush_tile;
74 void cursor_control(int key){
75 switch(key){
76 case LEFT_KEY: cursor_x--; break;
77 case RIGHT_KEY: cursor_x++; break;
78 case UP_KEY: cursor_y--; break;
79 case DOWN_KEY: cursor_y++; break;
82 if(cursor_x < 0){cursor_x = 0;}
83 if(cursor_y < 0){cursor_y = 0;}
85 /*check for crossing a zone exit*/
87 if(cursor_x < camera_x){
88 camera_x--;
89 point_camera(camera_x*16, camera_y*16);
91 if(cursor_x >= camera_x+20){
92 camera_x++;
93 point_camera(camera_x*16, camera_y*16);
95 if(cursor_y < camera_y){
96 camera_y--;
97 point_camera(camera_x*16, camera_y*16);
99 if(cursor_y >= camera_y+15){
100 camera_y++;
101 point_camera(camera_x*16, camera_y*16);
104 screen_x = cursor_x/20;
105 screen_y = cursor_y/15;
106 if(cursor_x < 0){screen_x--;}
107 if(cursor_y < 0){screen_y--;}
108 game.si = screen_x;
109 game.sj = screen_y;
111 cursor->x = cursor_x*16;
112 cursor->y = cursor_y*16;
114 if(hold_fire) press_fire();
118 void edit_keydown(int key){
120 if(key == ESCAPE_KEY){
121 end_program();
124 switch(key){
125 case LEFT_KEY:
126 case RIGHT_KEY:
127 case UP_KEY:
128 case DOWN_KEY:
129 repeat.flag = 1;
130 repeat.key = key;
131 repeat.timer = 0;
132 cursor_control(key);
133 break;
134 case FIRE_KEY:
135 hold_fire = 1;
136 press_fire();
137 break;
138 case L_KEY:
139 brush_tile--;
140 break;
141 case R_KEY:
142 brush_tile++;
143 break;
148 void edit_keyup(int key){
149 if(key == repeat.key){
150 repeat.flag = 0;
152 if(key == FIRE_KEY) hold_fire = 0;
155 void edit_joymovex(int joy, int x){
156 printf("you moved joystick %d x axis to %d\n",joy,x);
159 void edit_joymovey(int joy, int y){
160 printf("you moved joystick %d y axis to %d\n",joy,y);
163 void edit_joypress(int joy, int button){
164 printf("you pressed joystick %d button %d\n",joy,button);
167 void edit_joyrelease(int joy, int button){
168 printf("you released joystick %d button %d\n",joy,button);
172 void edit_draw(){
173 draw_stage();
174 draw_sprites();
175 printf_small(1,1,"zone: %s",game.zones[0]->name);
176 printf_small(1,10,"screen: %d,%d",screen_x,screen_y);
177 printf_small(1,19,"cursor: %d,%d",cursor_x,cursor_y);
178 printf_small(1,28,"camera: %d,%d",camera_x,camera_y);
181 struct handler edit_handler = {
182 edit_keydown, edit_keyup, edit_joymovex,
183 edit_joymovey, edit_joypress, edit_joyrelease
189 void main_init(int argc, char* argv[]){
190 backend_init(argc, argv);
191 loader_init();
192 graphics_init();
193 text_init();
195 game_setup();
197 set_handler(edit_handler);
198 game.update = NULL;
199 game.draw = edit_draw;
201 char** list = loader_readdir("zones");
202 int i;
203 for(i=0; list[i]; i++){
204 printf("loading zone \"%s\"\n",list[i]);
205 load_zone(list[i]);
207 loader_freedirlist(list);
209 /* dummy initialization */
210 game.current_zone = game.zones[0];
212 enable_stage(1);
214 cursor_x = 0;
215 cursor_y = 0;
216 load_sprite("box.spr",SPR_BOX);
217 cursor = enable_sprite(SPR_BOX);
219 camera_x = 0;
220 camera_y = 0;
221 point_camera(0,0);
223 hold_fire = 0;
227 void update(){
228 if(repeat.flag){
229 repeat.timer++;
230 if(repeat.timer > 40){
231 repeat.timer = 38;
232 cursor_control(repeat.key);
240 void main_quit(){
241 loader_quit();
242 backend_quit();
245 void main_loop(){
246 since();
247 int i;
248 int T = 0;
249 while(1){
250 T += since();
251 for(i=0; i<T/dt; i++){
252 input();
253 update();
255 if(T/dt > 0){
256 draw();
257 T %= dt;
259 if(ended()){break;}
260 delay(DELAY_AMOUNT);
264 int main(int argc, char* argv[]){
265 main_init(argc, argv);
266 main_loop();
267 main_quit();
268 return 0;