Improved input stuff again.
[cantaveria.git] / kernel.c
blobad7e6fcde6eb91c35ad5e088dc2405e5ae4420ed
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
22 evanrinehart@gmail.com
24 #include <stdlib.h>
26 #include <root.h>
27 #include <video.h>
28 #include <audio.h>
29 #include <input.h>
31 #include <loader.h>
32 #include <graphics.h>
33 #include <text.h>
35 #include <util.h>
37 #include <kernel.h>
38 #include <transfer.h>
40 int game_over = false;
41 int dt = QUANTUM;
43 static struct {
44 void (*update)();
45 void (*draw)();
46 void (*press)(input in);
47 void (*release)(input in);
48 } handler;
51 static void terminate(){
52 loader_quit();
53 audio_quit();
54 video_quit();
57 static void press(input in){ handler.press(in); }
58 static void release(input in){ handler.release(in); }
60 void dispatch_error(const char* msg){
61 report_error("kernel.c dispatch_input: %s\n", msg);
64 static void dispatch_input(){
65 input in = get_input();
66 while(in.type != NO_INPUT){
67 switch(in.type){
68 case BUTTON_PRESS: press(in); break;
69 case BUTTON_RELEASE: release(in); break;
70 case END_OF_PROGRAM: game_is_over(); break;
71 case SKIP_INPUT: dispatch_error("SKIP_INPUT is not supposed to come from generator"); break;
72 case INVALID_INPUT: dispatch_error("INVALID_INPUT produced in generator"); break;
73 case NO_INPUT: dispatch_error("NO_INPUT is impossible here"); break;
75 in = get_input();
79 void initialize(int argc, char* argv[]){
80 video_init(argc, argv);
81 audio_init();
82 input_init("FIXME");
83 loader_init();
84 graphics_init();
85 text_init();
86 rand_reset(RANDOM_SEED);
87 atexit(terminate);
89 setup_splash();
92 void update(){
93 dispatch_input();
94 fps_update();
95 // console_clear();
96 animate_sprites();
97 handler.update();
100 void draw(){
101 handler.draw();
102 draw_final();
105 void game_is_over(){
106 game_over = true;
109 int is_game_over(){
110 return game_over;
113 void set_handler(
114 void (*update)(),
115 void (*draw)(),
116 void (*press)(input in),
117 void (*release)(input in)
119 handler.update = update;
120 handler.draw = draw;
121 handler.press = press;
122 handler.release = release;