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
26 /* these graphics routines wrap video.c low level stuff */
44 sprite
* sprites
[MAX_SPRITES
];
47 animation
* animations
[MAX_ANIMATIONS
];
54 int stage_enabled
= 0;
60 minifont_gfx
= load_gfx("gfx/smallfont.tga");
61 for(i
=0; i
<MAX_ANIMATIONS
; i
++){
69 void draw_sprite(sprite
* spr
){
70 int x
= spr
->x
- camera_x();
71 int y
= spr
->y
- camera_y();
77 draw_gfx(g
, x
, y
, X
, Y
, W
, H
);
81 void draw_screen(zone* z, int si, int sj){
82 struct screen* scr = z->screens+si+z->w*sj;
84 int x = si*20*16 - camera.x;
85 int y = sj*15*16 - camera.y;
87 for(int j=0; j < 15; j++){
88 for(int i=0; i < 20; i++){
89 if(x > 320 || y > 240 || x < -16 || y < -16) continue;
90 int id = scr->tiles[i][j].id;
94 draw_gfx(G, x, y, X, Y, 16, 16);
114 void draw_small_text(char* str
, int x
, int y
){
119 draw_gfx_raw(minifont_gfx
, x
, y
, X
*4, Y
*9, 3, 8);
126 void printf_small(int x
, int y
, char* format
, ...){
129 va_start(ap
, format
);
130 vsnprintf(str
, 128, format
, ap
);
133 draw_small_text(str
, x
, y
);
141 for(i
=0; i
<sprite_count
; i
++){
142 draw_sprite(sprites
[i
]);
157 void animate_sprite(int i
){
158 sprite
* spr
= sprites
[i
];
160 // spr->frame_counter += dt;
161 animation
* ani
= animations
[spr
->anim
];
164 while(spr
->frame_counter
> ani
->frame_lens
[spr
->current_frame
]){
165 spr
->frame_counter
-= ani
->frame_lens
[spr
->current_frame
];
166 spr
->current_frame
++;
167 if(spr
->current_frame
== ani
->frame_count
){
168 spr
->current_frame
= 0;
170 spr
->frame
= ani
->frames
[spr
->current_frame
];
173 //if(spr->update) spr->update(spr, spr->userdata);
176 void animate_sprites(){
178 for(i
=0; i
<sprite_count
; i
++){
186 int load_sprite(char* path
, int id
){
189 reader
* rd
= loader_open(path
);
194 animation
* ani
= xmalloc(sizeof(animation
));
201 loader_scanline(rd
,"%256s",str
);
202 loader_scanline(rd
,"%d %d %d %d",&w
,&h
,&loop_mode
,&frame_count
);
204 ani
->frame_lens
= xmalloc(frame_count
*sizeof(short));
205 ani
->frames
= xmalloc(frame_count
*sizeof(struct frame
));
206 ani
->frame_count
= frame_count
;
208 int g
= load_gfx(str
);
216 int W
= gfx_width(g
);
217 int H
= gfx_height(g
);
221 for(i
=0; i
< frame_count
; i
++){
223 loader_scanline(rd
, "%d %d %d", &l
, &x
, &y
);
224 ani
->frame_lens
[i
] = l
;
225 ani
->frames
[i
].x
= x
;
226 ani
->frames
[i
].y
= y
;
227 ani
->frames
[i
].x0
= ((double)x
)/W
;
228 ani
->frames
[i
].y0
= ((double)y
)/H
;
229 ani
->frames
[i
].x1
= ((double)(x
+w
))/W
;
230 ani
->frames
[i
].y1
= ((double)(y
+h
))/W
;
234 animations
[id
] = ani
;
241 /********************/
242 /* graphics control */
243 /********************/
245 sprite
* enable_sprite(int sprnum
){
246 if(!animations
[sprnum
]){
247 fatal_error("enable_sprite: you just tried to enable sprite with type %d, which does not exist\n",sprnum
);
249 if(sprite_count
== MAX_SPRITES
){
250 /* need a priority based way to create important sprites if full */
253 sprite
* spr
= xmalloc(sizeof(sprite
));
254 animation
* ani
= animations
[sprnum
];
256 spr
->number
= sprite_count
;
257 spr
->frame_counter
= 0;
258 spr
->current_frame
= 0;
259 spr
->frame
= ani
->frames
[0];
260 spr
->gfxid
= ani
->gfxid
;
268 //spr->update = NULL;
269 //spr->userdata = NULL;
271 sprites
[sprite_count
++] = spr
;
275 void disable_sprite(sprite
* spr
){
276 sprite
* tmp
= sprites
[spr
->number
];
277 sprites
[spr
->number
] = sprites
[sprite_count
--];
281 sprite
* copy_sprite(sprite
* spr
){
282 if(sprite_count
== MAX_SPRITES
){
283 /* need way to make important sprites when full */
286 sprite
* copy
= xmalloc(sizeof(sprite
));
288 sprites
[sprite_count
++] = copy
;
294 void enable_stage(int yn
){
300 int load_bitmap(char* filename
){
301 int g
= load_gfx(filename
);
305 void draw_bitmap(int id
, int x
, int y
){
306 int W
= gfx_width(id
);
307 int H
= gfx_height(id
);
308 draw_gfx(id
, x
, y
, 0, 0, W
, H
);