Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | /* |
| 2 | * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
||
| 3 | * It is copyright by its individual contributors, as recorded in the |
||
| 4 | * project's Git history. See COPYING.txt at the top level for license |
||
| 5 | * terms and a link to the Git history. |
||
| 6 | */ |
||
| 7 | |||
| 8 | #include <stdlib.h> |
||
| 9 | #include <string.h> |
||
| 10 | #include <assert.h> |
||
| 11 | #include <string.h> |
||
| 12 | |||
| 13 | #ifdef _WIN32 |
||
| 14 | # include <windows.h> |
||
| 15 | #endif |
||
| 16 | |||
| 17 | #include <SDL.h> |
||
| 18 | |||
| 19 | #include "libmve.h" |
||
| 20 | |||
| 21 | static SDL_Surface *g_screen; |
||
| 22 | static palette_array_t g_palette; |
||
| 23 | static int g_truecolor; |
||
| 24 | |||
| 25 | static int doPlay(const char *filename); |
||
| 26 | |||
| 27 | static void usage(void) |
||
| 28 | { |
||
| 29 | fprintf(stderr, "usage: mveplay filename\n"); |
||
| 30 | exit(1); |
||
| 31 | } |
||
| 32 | |||
| 33 | int main(int c, char **v) |
||
| 34 | { |
||
| 35 | if (c != 2) |
||
| 36 | usage(); |
||
| 37 | |||
| 38 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) |
||
| 39 | { |
||
| 40 | fprintf(stderr, "Could not initialize SDL: %s\n",SDL_GetError()); |
||
| 41 | exit(1); |
||
| 42 | } |
||
| 43 | atexit(SDL_Quit); |
||
| 44 | |||
| 45 | return doPlay(v[1]); |
||
| 46 | } |
||
| 47 | |||
| 48 | static unsigned int fileRead(void *handle, void *buf, unsigned int count) |
||
| 49 | { |
||
| 50 | unsigned numread; |
||
| 51 | |||
| 52 | numread = fread(buf, 1, count, (FILE *)handle); |
||
| 53 | return (numread == count); |
||
| 54 | } |
||
| 55 | |||
| 56 | static void showFrame(unsigned char *buf, int dstx, int dsty, int bufw, int bufh, int sw, int sh) |
||
| 57 | { |
||
| 58 | int i; |
||
| 59 | unsigned char *pal; |
||
| 60 | SDL_Surface *sprite; |
||
| 61 | SDL_Rect srcRect, destRect; |
||
| 62 | |||
| 63 | if (dstx == -1) // center it |
||
| 64 | dstx = (sw - bufw) / 2; |
||
| 65 | if (dsty == -1) // center it |
||
| 66 | dsty = (sh - bufh) / 2; |
||
| 67 | |||
| 68 | if (g_truecolor) |
||
| 69 | sprite = SDL_CreateRGBSurfaceFrom(buf, bufw, bufh, 16, 2 * bufw, 0x7C00, 0x03E0, 0x001F, 0); |
||
| 70 | else |
||
| 71 | { |
||
| 72 | sprite = SDL_CreateRGBSurfaceFrom(buf, bufw, bufh, 8, bufw, 0x7C00, 0x03E0, 0x001F, 0); |
||
| 73 | |||
| 74 | pal = g_palette; |
||
| 75 | for(i = 0; i < 256; i++) |
||
| 76 | { |
||
| 77 | sprite->format->palette->colors[i].r = (*pal++) << 2; |
||
| 78 | sprite->format->palette->colors[i].g = (*pal++) << 2; |
||
| 79 | sprite->format->palette->colors[i].b = (*pal++) << 2; |
||
| 80 | sprite->format->palette->colors[i].unused = 0; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | srcRect.x = 0; |
||
| 85 | srcRect.y = 0; |
||
| 86 | srcRect.w = bufw; |
||
| 87 | srcRect.h = bufh; |
||
| 88 | destRect.x = dstx; |
||
| 89 | destRect.y = dsty; |
||
| 90 | destRect.w = bufw; |
||
| 91 | destRect.h = bufh; |
||
| 92 | |||
| 93 | SDL_BlitSurface(sprite, &srcRect, g_screen, &destRect); |
||
| 94 | |||
| 95 | if ( (g_screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) |
||
| 96 | SDL_Flip(g_screen); |
||
| 97 | else |
||
| 98 | SDL_UpdateRects(g_screen, 1, &destRect); |
||
| 99 | |||
| 100 | SDL_FreeSurface(sprite); |
||
| 101 | } |
||
| 102 | |||
| 103 | static void setPalette(unsigned char *p, unsigned start, unsigned count) |
||
| 104 | { |
||
| 105 | //Set color 0 to be black |
||
| 106 | g_palette[0] = g_palette[1] = g_palette[2] = 0; |
||
| 107 | |||
| 108 | //Set color 255 to be our subtitle color |
||
| 109 | g_palette[765] = g_palette[766] = g_palette[767] = 50; |
||
| 110 | |||
| 111 | //movie libs palette into our array |
||
| 112 | memcpy(g_palette + start*3, p+start*3, count*3); |
||
| 113 | } |
||
| 114 | |||
| 115 | static int pollEvents() |
||
| 116 | { |
||
| 117 | SDL_Event event; |
||
| 118 | |||
| 119 | while (SDL_PollEvent(&event)) |
||
| 120 | { |
||
| 121 | switch(event.type) |
||
| 122 | { |
||
| 123 | case SDL_QUIT: |
||
| 124 | case SDL_MOUSEBUTTONDOWN: |
||
| 125 | case SDL_MOUSEBUTTONUP: |
||
| 126 | return 1; |
||
| 127 | case SDL_KEYDOWN: |
||
| 128 | switch (event.key.keysym.sym) |
||
| 129 | { |
||
| 130 | case SDLK_ESCAPE: |
||
| 131 | case SDLK_q: |
||
| 132 | return 1; |
||
| 133 | case SDLK_f: |
||
| 134 | SDL_WM_ToggleFullScreen(g_screen); |
||
| 135 | break; |
||
| 136 | default: |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | break; |
||
| 140 | default: |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return 0; |
||
| 146 | } |
||
| 147 | |||
| 148 | static int doPlay(const char *filename) |
||
| 149 | { |
||
| 150 | int result; |
||
| 151 | int done = 0; |
||
| 152 | int bpp = 0; |
||
| 153 | FILE *mve; |
||
| 154 | MVE_videoSpec vSpec; |
||
| 155 | |||
| 156 | mve = fopen(filename, "rb"); |
||
| 157 | if (mve == NULL) { |
||
| 158 | fprintf(stderr, "can't open MVE file\n"); |
||
| 159 | return 1; |
||
| 160 | } |
||
| 161 | |||
| 162 | memset(g_palette, 0, 768); |
||
| 163 | |||
| 164 | MVE_sndInit(1); |
||
| 165 | MVE_memCallbacks(malloc, free); |
||
| 166 | MVE_ioCallbacks(fileRead); |
||
| 167 | MVE_sfCallbacks(showFrame); |
||
| 168 | MVE_palCallbacks(setPalette); |
||
| 169 | |||
| 170 | MVE_rmPrepMovie(mve, -1, -1, 1); |
||
| 171 | |||
| 172 | MVE_getVideoSpec(&vSpec); |
||
| 173 | |||
| 174 | bpp = vSpec.truecolor?16:8; |
||
| 175 | |||
| 176 | g_screen = SDL_SetVideoMode(vSpec.screenWidth, vSpec.screenHeight, bpp, SDL_ANYFORMAT); |
||
| 177 | |||
| 178 | g_truecolor = vSpec.truecolor; |
||
| 179 | |||
| 180 | while (!done && (result = MVE_rmStepMovie()) == 0) |
||
| 181 | { |
||
| 182 | done = pollEvents(); |
||
| 183 | } |
||
| 184 | |||
| 185 | MVE_rmEndMovie(); |
||
| 186 | |||
| 187 | fclose(mve); |
||
| 188 | |||
| 189 | return 0; |
||
| 190 | } |