Skip to content

Commit

Permalink
Update the library to use SDL2.
Browse files Browse the repository at this point in the history
  • Loading branch information
dulsi committed Jul 12, 2016
1 parent 4ad9ed6 commit 553a636
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ ETAGS = etags
CTAGS = ctags
CSCOPE = cscope
AM_RECURSIVE_TARGETS = cscope
am__DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/SDL_mng.pc.in README \
compile config.guess config.sub depcomp install-sh ltmain.sh \
missing
am__DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/SDL_mng.pc.in \
ChangeLog README compile config.guess config.sub depcomp \
install-sh ltmain.sh missing
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
distdir = $(PACKAGE)-$(VERSION)
top_distdir = $(distdir)
Expand Down
11 changes: 6 additions & 5 deletions SDL_mng.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
*/

#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_endian.h>
#include <SDL.h>
#include <SDL_surface.h>
#include <SDL_endian.h>
#include "SDL_mng.h"

#include <png.h>
Expand Down Expand Up @@ -225,7 +226,7 @@ MNG_Image *MNG_iterate_chunks(SDL_RWops *src)
{
/* Read MHDR chunk, and store in image struct */
case MNG_UINT_MHDR:
SDL_RWseek(src, -(current_chunk.chunk_size + 4), SEEK_CUR);
SDL_RWseek(src, SDL_RWtell(src) - (current_chunk.chunk_size + 4), SEEK_SET);
image->mhdr = read_MHDR(src);
frame_delay = 1000 / image->mhdr.Ticks_per_second;
break;
Expand All @@ -237,7 +238,7 @@ MNG_Image *MNG_iterate_chunks(SDL_RWops *src)

/* We've reached the end of a PNG - seek to IHDR and read */
case MNG_UINT_IEND:
SDL_RWseek(src, -byte_count, SEEK_CUR);
SDL_RWseek(src, SDL_RWtell(src)-byte_count, SEEK_SET);

/* We don't know how many frames there will be, really. */
if(start_frame == NULL)
Expand Down Expand Up @@ -382,7 +383,7 @@ SDL_Surface *MNG_read_frame(SDL_RWops *src)
Amask = 0x000000FF;
}

surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
surface = SDL_CreateRGBSurface(0, width, height,
32, Rmask, Gmask, Bmask, Amask);

if(surface == NULL)
Expand Down
2 changes: 1 addition & 1 deletion SDL_mng.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef __SDL_mng_h__
#define __SDL_mng_h__

#include <SDL/SDL.h>
#include <SDL.h>

#ifdef __cplusplus
extern "C" {
Expand Down
10 changes: 5 additions & 5 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -12189,7 +12189,7 @@ fi



SDL_VERSION=1.2.4
SDL_VERSION=2.0.0

# Check whether --with-sdl-prefix was given.
if test "${with_sdl_prefix+set}" = set; then :
Expand Down Expand Up @@ -12217,22 +12217,22 @@ fi
if test x$sdl_exec_prefix != x ; then
sdl_config_args="$sdl_config_args --exec-prefix=$sdl_exec_prefix"
if test x${SDL_CONFIG+set} != xset ; then
SDL_CONFIG=$sdl_exec_prefix/bin/sdl-config
SDL_CONFIG=$sdl_exec_prefix/bin/sdl2-config
fi
fi
if test x$sdl_prefix != x ; then
sdl_config_args="$sdl_config_args --prefix=$sdl_prefix"
if test x${SDL_CONFIG+set} != xset ; then
SDL_CONFIG=$sdl_prefix/bin/sdl-config
SDL_CONFIG=$sdl_prefix/bin/sdl2-config
fi
fi

as_save_PATH="$PATH"
if test "x$prefix" != xNONE; then
PATH="$prefix/bin:$prefix/usr/bin:$PATH"
fi
# Extract the first word of "sdl-config", so it can be a program name with args.
set dummy sdl-config; ac_word=$2
# Extract the first word of "sdl2-config", so it can be a program name with args.
set dummy sdl2-config; ac_word=$2
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
$as_echo_n "checking for $ac_word... " >&6; }
if ${ac_cv_path_SDL_CONFIG+:} false; then :
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ AC_PROG_CC
AC_PROG_INSTALL

dnl Check for libSDL
SDL_VERSION=1.2.4
SDL_VERSION=2.0.0
AM_PATH_SDL($SDL_VERSION,
:,
AC_MSG_ERROR([*** SDL version $SDL_VERSION not found!])
Expand Down
104 changes: 70 additions & 34 deletions mngtest.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
#include <SDL.h>
#include "SDL_mng.h"

SDL_Surface *init_SDL(void)
SDL_Renderer *init_SDL(void)
{
SDL_Surface *display;
SDL_Renderer *display;
int video_flags;

if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_TIMER) < 0)
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}

if(SDL_GetVideoInfo()->hw_available)
video_flags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
else
video_flags = SDL_SWSURFACE;

SDL_WM_SetCaption("MNG Test", "MNG Test");
SDL_Window *sdlWindow = SDL_CreateWindow("MNG Test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
400, 300,
0);
if(sdlWindow == NULL)
exit(1);

if((display = SDL_SetVideoMode(400, 300, 0, video_flags)) == NULL)
display = SDL_CreateRenderer(sdlWindow, -1, 0);
if(display == NULL)
exit(1);

return display;
}

int display_loop(SDL_Surface *display, MNG_Image *image)
Uint32 timerCallback(Uint32 interval, void *param)
{
SDL_Event event;
SDL_UserEvent userevent;

userevent.type = SDL_USEREVENT;
userevent.code = 0;
userevent.data1 = NULL;
userevent.data2 = NULL;

event.type = SDL_USEREVENT;
event.user = userevent;

SDL_PushEvent(&event);
return 0;
}

int display_loop(SDL_Renderer *display, MNG_Image *image)
{
SDL_Event event;
SDL_Texture *sdlTexture;
SDL_Surface *background;
SDL_Rect src_rect, dst_rect;
SDL_Surface *displayFake;
SDL_Rect src_rect;
SDL_TimerID timer;

unsigned long old_time, new_time, delay;
unsigned long elapsed_t = 0, frame = 0;
Expand All @@ -43,8 +65,20 @@ int display_loop(SDL_Surface *display, MNG_Image *image)
printf("frames: %i, fps: %i", image->frame_count, image->mhdr.Ticks_per_second);
#endif

background = SDL_CreateRGBSurface(SDL_SWSURFACE, display->w, display->h,
32, 0, 0, 0, 0);
sdlTexture = SDL_CreateTexture(display,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
400, 300);
displayFake = SDL_CreateRGBSurface(0, 400, 300, 32,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);
background = SDL_CreateRGBSurface(0, 400, 300, 32,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);

SDL_FillRect(background, NULL, SDL_MapRGB(background->format, 128, 128, 128));

Expand All @@ -53,32 +87,34 @@ int display_loop(SDL_Surface *display, MNG_Image *image)
src_rect.x = 0;
src_rect.y = 0;

dst_rect.w = image->frame[0]->w;
dst_rect.h = image->frame[0]->h;
dst_rect.x = (display->w - src_rect.w) / 2;
dst_rect.y = (display->h - src_rect.h) / 2;
MNG_AnimationState animation;
animation.animation = image;

new_time = SDL_GetTicks();
animation.dst.w = image->frame[0]->w;
animation.dst.h = image->frame[0]->h;
animation.dst.x = (400 - src_rect.w) / 2;
animation.dst.y = (300 - src_rect.h) / 2;
IMG_SetAnimationState(&animation, -1, 0);
old_time = SDL_GetTicks();

while(1)
{
old_time = new_time;
new_time = SDL_GetTicks();

elapsed_t += new_time - old_time;

if(elapsed_t >= delay)
unsigned long ticks = SDL_GetTicks();
SDL_Surface *nextImg = IMG_TimeUpdate(&animation, ticks);
if (nextImg)
{
elapsed_t -= delay;
if(++frame >= image->frame_count)
frame = 0;
SDL_BlitSurface(background, NULL, displayFake, NULL);
SDL_BlitSurface(nextImg, NULL, displayFake, &animation.dst);
SDL_UpdateTexture(sdlTexture, NULL, displayFake->pixels, 400 * sizeof (Uint32));
SDL_RenderClear(display);
SDL_RenderCopy(display, sdlTexture, NULL, NULL);
SDL_RenderPresent(display);
}
unsigned long animationDelay = IMG_TimeToNextFrame(&animation, ticks);
timer = SDL_AddTimer(animationDelay, timerCallback, NULL);

SDL_BlitSurface(background, NULL, display, NULL);
SDL_BlitSurface(image->frame[frame], NULL, display, &dst_rect);
SDL_Flip(display);

while(SDL_PollEvent(&event))
SDL_WaitEvent(&event);
SDL_RemoveTimer(timer);
{
switch(event.type)
{
Expand All @@ -92,7 +128,7 @@ int display_loop(SDL_Surface *display, MNG_Image *image)

int main(int argc, char **argv)
{
SDL_Surface *display;
SDL_Renderer *display;
MNG_Image *image;

if(argc > 1)
Expand Down

0 comments on commit 553a636

Please sign in to comment.