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
);
147 for(i
=0; i
<sprite_count
; i
++){
148 draw_sprite(sprites
[i
]);
162 void point_camera(int x
, int y
){
169 void animate_sprite(int i
){
170 sprite
* spr
= sprites
[i
];
172 // spr->frame_counter += dt;
173 animation
* ani
= animations
[spr
->anim
];
176 while(spr
->frame_counter
> ani
->frame_lens
[spr
->current_frame
]){
177 spr
->frame_counter
-= ani
->frame_lens
[spr
->current_frame
];
178 spr
->current_frame
++;
179 if(spr
->current_frame
== ani
->frame_count
){
180 spr
->current_frame
= 0;
182 spr
->frame
= ani
->frames
[spr
->current_frame
];
185 //if(spr->update) spr->update(spr, spr->userdata);
188 void animate_sprites(){
190 for(i
=0; i
<sprite_count
; i
++){
198 int load_sprite(char* filename
, int id
){
201 printf("loading %s\n",filename
);
203 char path
[1024] = "sprites/";
204 strncat(path
, filename
, 1023 - strlen(filename
));
206 reader
* rd
= loader_open(path
);
211 animation
* ani
= xmalloc(sizeof(animation
));
218 loader_scanline(rd
,"%256s",str
);
219 loader_scanline(rd
,"%d %d %d %d",&w
,&h
,&loop_mode
,&frame_count
);
221 ani
->frame_lens
= xmalloc(frame_count
*sizeof(short));
222 ani
->frames
= xmalloc(frame_count
*sizeof(struct frame
));
223 ani
->frame_count
= frame_count
;
225 int g
= load_gfx(str
);
233 int W
= gfx_width(g
);
234 int H
= gfx_height(g
);
238 for(i
=0; i
< frame_count
; i
++){
240 loader_scanline(rd
, "%d %d %d", &l
, &x
, &y
);
241 ani
->frame_lens
[i
] = l
;
242 ani
->frames
[i
].x
= x
;
243 ani
->frames
[i
].y
= y
;
244 ani
->frames
[i
].x0
= ((double)x
)/W
;
245 ani
->frames
[i
].y0
= ((double)y
)/H
;
246 ani
->frames
[i
].x1
= ((double)(x
+w
))/W
;
247 ani
->frames
[i
].y1
= ((double)(y
+h
))/W
;
251 animations
[id
] = ani
;
258 /********************/
259 /* graphics control */
260 /********************/
262 sprite
* enable_sprite(int sprnum
){
263 if(!animations
[sprnum
]){
264 fatal_error("enable_sprite: you just tried to enable sprite with type %d, which does not exist\n",sprnum
);
266 if(sprite_count
== MAX_SPRITES
){
267 /* need a priority based way to create important sprites if full */
270 sprite
* spr
= xmalloc(sizeof(sprite
));
271 animation
* ani
= animations
[sprnum
];
273 spr
->number
= sprite_count
;
274 spr
->frame_counter
= 0;
275 spr
->current_frame
= 0;
276 spr
->frame
= ani
->frames
[0];
277 spr
->gfxid
= ani
->gfxid
;
285 //spr->update = NULL;
286 //spr->userdata = NULL;
288 sprites
[sprite_count
++] = spr
;
292 void disable_sprite(sprite
* spr
){
293 sprite
* tmp
= sprites
[spr
->number
];
294 sprites
[spr
->number
] = sprites
[sprite_count
--];
298 sprite
* copy_sprite(sprite
* spr
){
299 if(sprite_count
== MAX_SPRITES
){
300 /* need way to make important sprites when full */
303 sprite
* copy
= xmalloc(sizeof(sprite
));
305 sprites
[sprite_count
++] = copy
;
311 void enable_stage(int yn
){
317 int load_bitmap(char* filename
){
318 int g
= load_gfx(filename
);
322 void draw_bitmap(int id
, int x
, int y
){
323 int W
= gfx_width(id
);
324 int H
= gfx_height(id
);
325 draw_gfx(id
, x
, y
, 0, 0, W
, H
);