239 lines
7.0 KiB
C++
Executable File
239 lines
7.0 KiB
C++
Executable File
#include "jdraw8.h"
|
|
#include "SDL2/SDL_image.h"
|
|
#include "SDL2/SDL_opengl.h"
|
|
#include "jfile.h"
|
|
#include <fstream>
|
|
|
|
JD8_Surface screen = NULL;
|
|
JD8_Palette main_palette = NULL;
|
|
Uint32* pixel_data = NULL;
|
|
|
|
int screenWidth = 320;
|
|
int screenHeight = 200;
|
|
|
|
Uint32 contadorFPS = 0;
|
|
Uint32 tempsFPS = SDL_GetTicks();
|
|
char *fps = (char *)malloc(10);
|
|
|
|
SDL_GLContext gContext;
|
|
SDL_Window* window = NULL;
|
|
SDL_Surface* screenSurface = NULL;
|
|
|
|
void JD8_Init(char *title) {
|
|
screen = (JD8_Surface)calloc( 1, 64000 );
|
|
main_palette = (JD8_Palette)calloc( 1, 768 );
|
|
pixel_data = (Uint32*)calloc( 1, 1048576 );
|
|
|
|
//SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
|
|
|
|
window = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
|
|
gContext = SDL_GL_CreateContext( window );
|
|
//if( SDL_GL_SetSwapInterval( 1 ) < 0 ) { printf( "Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError() ); }
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
|
|
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
|
|
|
|
glClearColor( 0, 0, 0, 0 );
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
|
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
|
glMatrixMode( GL_PROJECTION );
|
|
glLoadIdentity();
|
|
glOrtho( 0, 320, 200, 0, -1, 1 );
|
|
glMatrixMode( GL_MODELVIEW );
|
|
glLoadIdentity();
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
GLuint textureName;
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
glGenTextures(1, &textureName);
|
|
glBindTexture(GL_TEXTURE_2D, textureName);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);
|
|
}
|
|
|
|
void JD8_Quit() {
|
|
if( screen != NULL ) free( screen );
|
|
if( main_palette != NULL ) free( main_palette );
|
|
if( pixel_data != NULL ) free( pixel_data );
|
|
SDL_DestroyWindow( window );
|
|
}
|
|
|
|
void JD8_ClearScreen(Uint8 color) {
|
|
memset( screen, color, 64000 );
|
|
}
|
|
|
|
JD8_Surface JD8_NewSurface() {
|
|
JD8_Surface surface = (JD8_Surface)malloc( 64000 );
|
|
memset( surface, 0, 64000 );
|
|
return surface;
|
|
}
|
|
|
|
JD8_Surface JD8_LoadSurface(char *file) {
|
|
int filesize = 0;
|
|
char *buffer = JF_GetBufferFromResource(file, filesize);
|
|
|
|
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
|
|
SDL_Surface *temp = IMG_Load_RW(rw, 1);
|
|
|
|
free(buffer);
|
|
|
|
if (temp == NULL) {
|
|
printf("Unable to load bitmap: %s\n", SDL_GetError());
|
|
exit(1);
|
|
}
|
|
|
|
JD8_Surface image = JD8_NewSurface();
|
|
memcpy( image, temp->pixels, 64000 );
|
|
|
|
SDL_FreeSurface(temp);
|
|
return image;
|
|
}
|
|
|
|
JD8_Palette JD8_LoadPalette(char *file) {
|
|
int filesize = 0;
|
|
char *buffer = NULL;
|
|
buffer = JF_GetBufferFromResource(file, filesize);
|
|
|
|
SDL_RWops *rw = NULL;
|
|
rw = SDL_RWFromMem(buffer, filesize);
|
|
if( rw == NULL ) { printf("ERROR: No s'ha pogut crear el RWops"); exit(1); }
|
|
|
|
SDL_Surface *temp = NULL;
|
|
temp = IMG_Load_RW(rw, 1);
|
|
if( temp == NULL ) { printf("ERROR! No s'ha pogut crear la SDL_Surface: %s", SDL_GetError()); exit(1); }
|
|
|
|
free(buffer);
|
|
|
|
JD8_Palette palette = (JD8_Palette)malloc( 768 );
|
|
|
|
for( int i = 0; i < 256; i++ ) {
|
|
palette[i].r = temp->format->palette->colors[i].r;
|
|
palette[i].g = temp->format->palette->colors[i].g;
|
|
palette[i].b = temp->format->palette->colors[i].b;
|
|
}
|
|
|
|
SDL_FreeSurface(temp);
|
|
|
|
return palette;
|
|
}
|
|
|
|
void JD8_SetScreenPalette(JD8_Palette palette) {
|
|
if( main_palette != NULL) free( main_palette );
|
|
main_palette = palette;
|
|
}
|
|
|
|
void JD8_Blit(JD8_Surface surface) {
|
|
memcpy( screen, surface, 64000 );
|
|
}
|
|
|
|
void JD8_Blit(int x, int y, JD8_Surface surface, int sx, int sy, int sw, int sh) {
|
|
int src_pointer = sx + (sy*320);
|
|
int dst_pointer = x + (y*320);
|
|
for( int i = 0; i < sh; i++ ) {
|
|
memcpy( &screen[dst_pointer], &surface[src_pointer], sw );
|
|
src_pointer += 320;
|
|
dst_pointer += 320;
|
|
}
|
|
}
|
|
|
|
void JD8_BlitToSurface(int x, int y, JD8_Surface surface, int sx, int sy, int sw, int sh, JD8_Surface dest) {
|
|
int src_pointer = sx + (sy*320);
|
|
int dst_pointer = x + (y*320);
|
|
for( int i = 0; i < sh; i++ ) {
|
|
memcpy( &dest[dst_pointer], &surface[src_pointer], sw );
|
|
src_pointer += 320;
|
|
dst_pointer += 320;
|
|
}
|
|
}
|
|
|
|
void JD8_BlitCK(int x, int y, JD8_Surface surface, int sx, int sy, int sw, int sh, Uint8 colorkey ) {
|
|
int src_pointer = sx + (sy*320);
|
|
int dst_pointer = x + (y*320);
|
|
for( int j = 0; j < sh; j++ ) {
|
|
for( int i = 0; i < sw; i++ ) {
|
|
if( surface[src_pointer+i] != colorkey ) screen[dst_pointer+i] = surface[src_pointer+i];
|
|
}
|
|
src_pointer += 320;
|
|
dst_pointer += 320;
|
|
}
|
|
}
|
|
|
|
void JD8_BlitCKToSurface(int x, int y, JD8_Surface surface, int sx, int sy, int sw, int sh, JD8_Surface dest, Uint8 colorkey ) {
|
|
int src_pointer = sx + (sy*320);
|
|
int dst_pointer = x + (y*320);
|
|
for( int j = 0; j < sh; j++ ) {
|
|
for( int i = 0; i < sw; i++ ) {
|
|
if( surface[src_pointer+i] != colorkey ) dest[dst_pointer+i] = surface[src_pointer+i];
|
|
}
|
|
src_pointer += 320;
|
|
dst_pointer += 320;
|
|
}
|
|
}
|
|
|
|
void JD8_Flip() {
|
|
for( int x = 0; x < 320; x++ ) {
|
|
for( int y = 0; y < 200; y++ ) {
|
|
Uint32 color = 0xFF000000 + main_palette[screen[x + ( y * 320 )]].r + ( main_palette[screen[x + ( y * 320 )]].g << 8 ) + ( main_palette[screen[x + ( y * 320 )]].b << 16 );
|
|
pixel_data[y + ( x * 512 )] = color;
|
|
}
|
|
}
|
|
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);
|
|
|
|
glBegin( GL_QUADS );
|
|
glTexCoord2f( 0.0, 0.0 );
|
|
glVertex3f( 0.0, 0.0, 0.0 );
|
|
|
|
glTexCoord2f( 0.0, 1.0 );
|
|
glVertex3f( 512.0, 0.0, 0.0 );
|
|
|
|
glTexCoord2f( 1.0, 1.0 );
|
|
glVertex3f( 512.0, 512.0, 0.0 );
|
|
|
|
glTexCoord2f( 1.0, 0.0 );
|
|
glVertex3f( 0.0, 512.0, 0.0 );
|
|
glEnd();
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
}
|
|
|
|
void JD8_FreeSurface(JD8_Surface surface) {
|
|
free( surface );
|
|
}
|
|
|
|
Uint8 JD8_GetPixel( JD8_Surface surface, int x, int y ) {
|
|
return surface[x + (y*320)];
|
|
}
|
|
|
|
void JD8_PutPixel( JD8_Surface surface, int x, int y, Uint8 pixel ) {
|
|
surface[x + (y*320)] = pixel;
|
|
}
|
|
|
|
void JD8_FadeOut() {
|
|
for( int j = 0; j < 32; j++ ) {
|
|
for( int i = 0; i < 256; i++ ) {
|
|
if( main_palette[i].r >= 8 ) main_palette[i].r-=8; else main_palette[i].r=0;
|
|
if( main_palette[i].g >= 8 ) main_palette[i].g-=8; else main_palette[i].g=0;
|
|
if( main_palette[i].b >= 8 ) main_palette[i].b-=8; else main_palette[i].b=0;
|
|
}
|
|
JD8_Flip();
|
|
}
|
|
}
|
|
|
|
#define MAX(a, b) (a) > (b) ? (a) : (b)
|
|
|
|
void JD8_FadeToPal( JD8_Palette pal ) {
|
|
for( int j = 0; j < 32; j++ ) {
|
|
for( int i = 0; i < 256; i++ ) {
|
|
if( main_palette[i].r <= int(pal[i].r)-8 ) main_palette[i].r+=8; else main_palette[i].r=pal[i].r;
|
|
if( main_palette[i].g <= int(pal[i].g)-8 ) main_palette[i].g+=8; else main_palette[i].g=pal[i].g;
|
|
if( main_palette[i].b <= int(pal[i].b)-8 ) main_palette[i].b+=8; else main_palette[i].b=pal[i].b;
|
|
}
|
|
JD8_Flip();
|
|
}
|
|
}
|