fix regression: enemy soldiers fired in wrong direction
[rofl0r-openDOW.git] / mission_select.c
blob5d7ac68a91b4bdb4ff982abc37584f278105ec94
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) {
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 p = vecadd(&maps[i]->worldmap_coords, &VEC(2,2));
22 p.x *= SCALE;
23 p.y *= SCALE;
24 p = vecadd(&p, &VEC(SCALE/2,SCALE/2));
25 if(vecdist(&p, &c) <= 3*SCALE) { ret = i; goto done; }
27 done:
28 return ret;
31 static void draw_blinky(const struct palpic *b, int x, int y) {
32 enum map_index i;
33 vec2f p;
34 for(i = 0; i < MI_MAX; i++) {
35 p = maps[i]->worldmap_coords;
36 blit_sprite((x+p.x)*SCALE, (y+p.y)*SCALE, &video, SCALE, b, map_ticks%7, 0);
40 static void draw_face(const struct map *map) {
41 #define FACE_X 10
42 #define FACE_Y 10
43 #define FACE_H 32
44 #define FACE_W 32
45 blit_sprite(FACE_X*SCALE, FACE_Y*SCALE, &video, SCALE, spritemaps[SI_CLIENTS], map->client_face, 0);
48 static void draw_maptext(const struct map *map) {
49 unsigned i;
50 #define TEXT_X 48
51 #define TEXT_Y (FACE_Y - 6)
52 #define TEXT_H 8
53 for(i = 0; i < 5; i++) {
54 font_print(TEXT_X*SCALE, TEXT_Y*SCALE + i*(TEXT_H+1)*SCALE,
55 map->mission_text[i], 33, SCALE, PRGB(0xdd,0xbb,0x00));
59 static void draw_frame() {
60 blit_sprite((FACE_X-2)*SCALE, (FACE_Y-2)*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_HORIZ], 0, 0);
61 blit_sprite((FACE_X-2)*SCALE, (FACE_Y+FACE_H)*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_HORIZ], 1, 0);
62 blit_sprite((FACE_X-2)*SCALE, FACE_Y*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_VERT], 0, 0);
63 blit_sprite((FACE_X+FACE_W)*SCALE, FACE_Y*SCALE, &video, SCALE, spritemaps[SI_PICFRAME_VERT], 1, 0);
66 static void draw_stuff(const struct palpic *world, int x, int y) {
67 clear_screen();
68 draw_world(world, x, y);
69 const struct palpic *blinky = spritemaps[SI_MAPBLINK];
70 draw_blinky(blinky, x, y);
71 enum map_index m = cursor_on_map(x, y);
72 if(m != MI_INVALID) {
73 const struct map *map = maps[m];
74 draw_frame();
75 draw_face(map);
76 draw_maptext(map);
78 video_update();
81 static void map_tick(const struct palpic *world, int x, int y) {
82 static unsigned tc;
83 draw_stuff(world, x, y);
84 if(tc%6==0) map_ticks++;
85 tc++;
86 if (audio_process() == -1) music_restart();
87 SDL_Delay(20);
89 #include <stdio.h>
90 enum map_index choose_mission() {
91 const struct palpic *world = spritemaps[SI_WORLDMAP];
92 int x = (320 - palpic_getspritewidth(world))/2;
93 int y = ((240 - palpic_getspriteheight(world))/2)+16;
94 enum map_index ret = MI_INVALID;
96 SDL_Event sdl_event;
97 while(1) {
98 while (SDL_PollEvent(&sdl_event)) {
99 ret = MI_INVALID;
100 switch (sdl_event.type) {
101 case SDL_MOUSEMOTION:
102 cursor.x = sdl_event.motion.x;
103 cursor.y = sdl_event.motion.y;
104 break;
105 case SDL_MOUSEBUTTONDOWN:
106 dprintf(2, "click on %d,%d\n", cursor.x/SCALE-x, cursor.y/SCALE-y);
107 if((ret = cursor_on_map(x, y)) != MI_INVALID) goto dun_goofed;
108 break;
109 case SDL_QUIT:
110 goto dun_goofed;
111 case SDL_KEYDOWN:
112 switch(sdl_event.key.keysym.sym) {
113 case SDLK_RETURN:
114 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
115 (sdl_event.key.keysym.mod & KMOD_RALT)) {
116 toggle_fullscreen();
117 SDL_Delay(1);
119 break;
120 case SDLK_ESCAPE:
121 goto dun_goofed;
122 default:
123 break;
125 default:
126 break;
130 map_tick(world, x, y);
132 dun_goofed:
133 clear_screen();
134 video_update();
135 return ret;