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 */
45 sprite
* sprites
[MAX_SPRITES
];
48 animation
* animations
[MAX_ANIMATIONS
];
55 int stage_enabled
= 0;
66 minifont_gfx
= load_gfx("smallfont.tga");
67 for(i
=0; i
<MAX_ANIMATIONS
; i
++){
75 void draw_sprite(sprite
* spr
){
76 int x
= spr
->x
- camera
.x
;
77 int y
= spr
->y
- camera
.y
;
83 draw_gfx(g
, x
, y
, X
, Y
, W
, H
);
87 void draw_screen(zone* z, int si, int sj){
88 struct screen* scr = z->screens+si+z->w*sj;
90 int x = si*20*16 - camera.x;
91 int y = sj*15*16 - camera.y;
93 for(int j=0; j < 15; j++){
94 for(int i=0; i < 20; i++){
95 if(x > 320 || y > 240 || x < -16 || y < -16) continue;
96 int id = scr->tiles[i][j].id;
100 draw_gfx(G, x, y, X, Y, 16, 16);
120 void draw_small_text(char* str
, int x
, int y
){
125 draw_gfx_raw(minifont_gfx
, x
, y
, X
*4, Y
*9, 3, 8);
132 void printf_small(int x
, int y
, char* format
, ...){
135 va_start(ap
, format
);
136 vsnprintf(str
, 128, format
, ap
);
139 draw_small_text(str
, x
, y
);
145 void draw_screen(zone
* z
, int si
, int sj
){
147 struct screen
* scr
= ZONE_LOOKUP(z
,si
,sj
);
149 int G
= z
->tileset_gfx
;
150 int x
= si
*20*16 - camera
.x
;
151 int y
= sj
*15*16 - camera
.y
;
154 for(j
=0; j
< 15; j
++){
155 for(i
=0; i
< 20; i
++){
156 int id
= scr
->tiles
[i
][j
];
160 draw_gfx(G
, x
, y
, X
, Y
, 16, 16);
163 int X
= x
+camera
.x
/2;
205 zone
* z
= game
.current_zone
;
210 { 1,0},{ 1, 1},{0, 1},{-1, 1},
211 {-1,0},{-1,-1},{0,-1},{ 1,-1}
214 draw_screen(z
, si
, sj
);
216 draw_screen(z
, si
+table
[i
][0], sj
+table
[i
][1]);
222 for(i
=0; i
<sprite_count
; i
++){
223 draw_sprite(sprites
[i
]);
237 void point_camera(int x
, int y
){
244 void animate_sprite(int i
){
245 sprite
* spr
= sprites
[i
];
247 // spr->frame_counter += dt;
248 animation
* ani
= animations
[spr
->anim
];
251 while(spr
->frame_counter
> ani
->frame_lens
[spr
->current_frame
]){
252 spr
->frame_counter
-= ani
->frame_lens
[spr
->current_frame
];
253 spr
->current_frame
++;
254 if(spr
->current_frame
== ani
->frame_count
){
255 spr
->current_frame
= 0;
257 spr
->frame
= ani
->frames
[spr
->current_frame
];
260 //if(spr->update) spr->update(spr, spr->userdata);
263 void animate_sprites(){
265 for(i
=0; i
<sprite_count
; i
++){
273 int load_sprite(char* filename
, int id
){
276 printf("loading %s\n",filename
);
278 char path
[1024] = "sprites/";
279 strncat(path
, filename
, 1023 - strlen(filename
));
281 reader
* rd
= loader_open(path
);
286 animation
* ani
= xmalloc(sizeof(animation
));
293 loader_scanline(rd
,"%256s",str
);
294 loader_scanline(rd
,"%d %d %d %d",&w
,&h
,&loop_mode
,&frame_count
);
296 ani
->frame_lens
= xmalloc(frame_count
*sizeof(short));
297 ani
->frames
= xmalloc(frame_count
*sizeof(struct frame
));
298 ani
->frame_count
= frame_count
;
300 int g
= load_gfx(str
);
308 int W
= gfx_width(g
);
309 int H
= gfx_height(g
);
313 for(i
=0; i
< frame_count
; i
++){
315 loader_scanline(rd
, "%d %d %d", &l
, &x
, &y
);
316 ani
->frame_lens
[i
] = l
;
317 ani
->frames
[i
].x
= x
;
318 ani
->frames
[i
].y
= y
;
319 ani
->frames
[i
].x0
= ((double)x
)/W
;
320 ani
->frames
[i
].y0
= ((double)y
)/H
;
321 ani
->frames
[i
].x1
= ((double)(x
+w
))/W
;
322 ani
->frames
[i
].y1
= ((double)(y
+h
))/W
;
326 animations
[id
] = ani
;
333 /********************/
334 /* graphics control */
335 /********************/
337 sprite
* enable_sprite(int sprnum
){
338 if(!animations
[sprnum
]){
339 fatal_error("enable_sprite: you just tried to enable sprite with type %d, which does not exist\n",sprnum
);
341 if(sprite_count
== MAX_SPRITES
){
342 /* need a priority based way to create important sprites if full */
345 sprite
* spr
= xmalloc(sizeof(sprite
));
346 animation
* ani
= animations
[sprnum
];
348 spr
->number
= sprite_count
;
349 spr
->frame_counter
= 0;
350 spr
->current_frame
= 0;
351 spr
->frame
= ani
->frames
[0];
352 spr
->gfxid
= ani
->gfxid
;
360 //spr->update = NULL;
361 //spr->userdata = NULL;
363 sprites
[sprite_count
++] = spr
;
367 void disable_sprite(sprite
* spr
){
368 sprite
* tmp
= sprites
[spr
->number
];
369 sprites
[spr
->number
] = sprites
[sprite_count
--];
373 sprite
* copy_sprite(sprite
* spr
){
374 if(sprite_count
== MAX_SPRITES
){
375 /* need way to make important sprites when full */
378 sprite
* copy
= xmalloc(sizeof(sprite
));
380 sprites
[sprite_count
++] = copy
;
386 void enable_stage(int yn
){
392 int load_bitmap(char* filename
){
393 int g
= load_gfx(filename
);
397 void draw_bitmap(int id
, int x
, int y
){
398 int W
= gfx_width(id
);
399 int H
= gfx_height(id
);
400 draw_gfx(id
, x
, y
, 0, 0, W
, H
);