Skip to content

Commit 4701173

Browse files
committed
Add fifsdl
1 parent ca77ae1 commit 4701173

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
echo "Building fif2png"
23
g++ -c fif2png.cpp -O2 -Wall `libpng-config --cflags`
34
g++ fif2png.o -o fif2png `libpng-config --ldflags`
45
rm fif2png.o
6+
echo "Building fifsdl"
7+
g++ fifsdl.cpp -O2 -Wall -o fifsdl -lSDL2

fifsdl.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//FastIF SDL viewer
2+
//Copyright (C) 2020 Extrasklep
3+
4+
#include <iostream>
5+
#include <fstream>
6+
#include <SDL2/SDL.h>
7+
#include "fif_decoder.h"
8+
9+
SDL_Window* window = NULL;
10+
SDL_Surface* screenSurface = NULL;
11+
12+
unsigned int XRES, YRES;
13+
14+
void setpixelsdl(SDL_Surface *surface, int x, int y, uint32_t pixel) {
15+
uint8_t *target_pixel = (uint8_t *)surface->pixels + y * surface->pitch + x * 4;
16+
*(uint32_t *)target_pixel = pixel;
17+
}
18+
19+
long map(long x, long in_min, long in_max, long out_min, long out_max) {
20+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
21+
}
22+
23+
uint32_t mapRGB(int r, int g, int b) {return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);}
24+
25+
bool init(const char* title) {
26+
//Initialize SDL
27+
if(SDL_Init( SDL_INIT_VIDEO ) < 0) {
28+
std::cout << "SDL init error: " << SDL_GetError();
29+
}
30+
else {
31+
//Create window
32+
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, XRES, YRES, SDL_WINDOW_SHOWN);
33+
if(window == NULL) {
34+
std::cout << "SDL window error: " << SDL_GetError();
35+
}
36+
else {
37+
//Get window surface
38+
screenSurface = SDL_GetWindowSurface(window);
39+
//Update the surface
40+
SDL_UpdateWindowSurface(window);
41+
}
42+
}
43+
return 0;
44+
}
45+
46+
void quit() {
47+
SDL_DestroyWindow(window);
48+
SDL_Quit();
49+
}
50+
51+
int main(int argc, char** args) {
52+
if(argc<2) {
53+
std::cout << "Usage: " << args[0] << " [FIF input file]\n";
54+
return 0;
55+
}
56+
57+
FIF* fif = new FIF;
58+
std::ifstream ifile(args[1],std::ios::in|std::ios::binary|std::ios::ate);
59+
if(!ifile.is_open()) {
60+
perror(args[1]);
61+
exit(1);
62+
} else {
63+
std::streampos size = ifile.tellg();
64+
ifile.seekg(0);
65+
fif->data = new unsigned char[size];
66+
ifile.read((char*)fif->data,size);
67+
ifile.close();
68+
}
69+
70+
//Read FIF
71+
signed int res = FIF_read(fif);
72+
if(res < 0) {
73+
std::cout << "FIF error " << (int)res << ".\n";
74+
exit(2);
75+
}
76+
XRES = fif->width;
77+
YRES = fif->height;
78+
unsigned long vbufsize = fif->width*fif->height;
79+
80+
bool quitRequest=false;
81+
SDL_Event sdlEvent;
82+
83+
init("FIF viewer");
84+
85+
while(quitRequest==0) {
86+
//Handle events on queue
87+
while(SDL_PollEvent( &sdlEvent ) != 0) {
88+
//Quit request
89+
if( sdlEvent.type == SDL_QUIT ) {quitRequest = true;}
90+
}
91+
92+
//Play FIF
93+
if(!fif->eof) {
94+
signed int res = FIF_read(fif);
95+
if(res == 0) {
96+
std::cout << "Playback done\n";
97+
SDL_Delay(50);
98+
} else if(res < 0) {
99+
std::cout << "FIF error " << (int)res << ".\n";
100+
exit(2);
101+
} else if(res > 0) {
102+
SDL_Delay(res);
103+
}
104+
} else {
105+
SDL_Delay(50);
106+
}
107+
108+
//Write image data to SDL
109+
for(unsigned long i=0;i<vbufsize;i++) {
110+
((uint32_t*)screenSurface->pixels)[i] = mapRGB(fif->decoded_data[i].r,fif->decoded_data[i].g,fif->decoded_data[i].b);
111+
}
112+
113+
SDL_UpdateWindowSurface( window );
114+
}
115+
116+
quit();
117+
118+
FIF_free(fif);
119+
delete fif;
120+
121+
return 0;
122+
}

0 commit comments

Comments
 (0)