palpic2png.c: improve, make usable with ppic binary files
[rofl0r-openDOW.git] / mission_select.c
blobfe47560950a091e551e36118482ae23aad59ba32
1 #include "video.h"
2 #include "palpic.h"
3 #include "spritemaps.h"
4 #include "maps.h"
5 #include "font.h"
6 #include "audio.h"
7 #include "music.h"
8 #include "SDL/SDL.h"
10 static void draw_world(const struct palpic *w, int x, int y) {
11 blit_sprite(x*SCALE, y*SCALE, &video, SCALE, w, 0, 0);
14 static unsigned map_ticks;
15 static struct { int x, y; } cursor;
16 static enum map_index cursor_on_map(int x, int y, uint8_t*completed) {
17 enum map_index i, ret = MI_INVALID;
18 vec2f c = { .x = cursor.x - x*SCALE, .y = cursor.y - y*SCALE};
19 vec2f p;
20 for(i = 0; i < MI_MAX; i++) {
21 if(completed[i]) continue;
22 p = vecadd(&maps[i]->worldmap_coords, &VEC(2,2));
23 p.x *= SCALE;
24 p.y *= SCALE;
25 p = vecadd(&p, &VEC(SCALE/2,SCALE/2));
26 if(vecdist(&p, &c) <= 3*SCALE) { ret = i; goto done; }
28 done:
29 return ret;
32 static void draw_blinky(const struct palpic *b, int x, int y, uint8_t*completed) {
33 enum map_index i;
34 vec2f p;
35 for(i = 0; i < MI_MAX; i++) {
36 if(completed[i]) continue;
37 p = maps[i]->worldmap_coords;
38 blit_sprite((x+p.x)*SCALE, (y+p.y)*SCALE, &video, SCALE, b, map_ticks%7, 0);
42 static void draw_face(const struct map *map) {
43 #define FACE_X 10
44 #define FACE_Y 10
45 #define FACE_H 32
46 #define FACE_W 32
47 blit_sprite(FACE_X*SCALE, FACE_Y*SCALE, &video, SCALE, spritemaps[SI_CLIENTS], map->client_face, 0);
50 static void draw_maptext(const struct map *map) {
51 unsigned i;
52 #define TEXT_X 48
53 #define TEXT_Y (FACE_Y - 6)
54 #define TEXT_H 8
55 for(i = 0; i < 5; i++) {
56 font_print(TEXT_X*SCALE, TEXT_Y*SCALE + i*(TEXT_H+1)*SCALE,
57 map->mission_text[i], 33, SCALE, PRGB(0xdd,0xbb,0x00));
61 static void draw_frame() {
62 blit_sprite((FACE_X-2)*SCALE, (FACE_Y-2)*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_HORIZ], 0, 0);
63 blit_sprite((FACE_X-2)*SCALE, (FACE_Y+FACE_H)*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_HORIZ], 1, 0);
64 blit_sprite((FACE_X-2)*SCALE, FACE_Y*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_VERT], 0, 0);
65 blit_sprite((FACE_X+FACE_W)*SCALE, FACE_Y*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_VERT], 1, 0);
68 static void draw_stuff(const struct palpic *world, int x, int y, uint8_t *completed) {
69 clear_screen();
70 draw_world(world, x, y);
71 const struct palpic *blinky = spritemaps[SI_MAPBLINK];
72 draw_blinky(blinky, x, y, completed);
73 enum map_index m = cursor_on_map(x, y, completed);
74 if(m != MI_INVALID) {
75 const struct map *map = maps[m];
76 draw_frame();
77 draw_face(map);
78 draw_maptext(map);
80 video_update();
83 static void map_tick(const struct palpic *world, int x, int y, uint8_t *completed) {
84 static unsigned tc;
85 draw_stuff(world, x, y, completed);
86 if(tc%6==0) map_ticks++;
87 tc++;
88 if (audio_process() == -1) music_restart();
89 SDL_Delay(20);
91 #include <stdio.h>
92 enum map_index choose_mission(uint8_t* completed) {
93 const struct palpic *world = spritemaps[SI_WORLDMAP];
94 int x = (320 - palpic_getspritewidth(world))/2;
95 int y = ((240 - palpic_getspriteheight(world))/2)+16;
96 enum map_index ret = MI_INVALID;
98 SDL_Event sdl_event;
99 while(1) {
100 while (SDL_PollEvent(&sdl_event)) {
101 ret = MI_INVALID;
102 switch (sdl_event.type) {
103 case SDL_MOUSEMOTION:
104 cursor.x = sdl_event.motion.x;
105 cursor.y = sdl_event.motion.y;
106 break;
107 case SDL_MOUSEBUTTONDOWN:
108 dprintf(2, "click on %d,%d\n", cursor.x/SCALE-x, cursor.y/SCALE-y);
109 if((ret = cursor_on_map(x, y, completed)) != MI_INVALID) goto dun_goofed;
110 break;
111 case SDL_QUIT:
112 goto dun_goofed;
113 case SDL_KEYDOWN:
114 switch(sdl_event.key.keysym.sym) {
115 case SDLK_RETURN:
116 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
117 (sdl_event.key.keysym.mod & KMOD_RALT)) {
118 toggle_fullscreen();
119 SDL_Delay(1);
121 break;
122 case SDLK_ESCAPE:
123 goto dun_goofed;
124 default:
125 break;
127 default:
128 break;
132 map_tick(world, x, y, completed);
134 dun_goofed:
135 clear_screen();
136 video_update();
137 return ret;