#include <stdlib.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <SDL.h>
/
SDL_Surface *g_screenSurface = NULL;
SDL_Surface *g_donutSurface = NULL;
SDL_Surface *g_backface = NULL;
SDL_Surface *g_test = NULL;
//特效表面
SDL_Surface *bBuffer;
SDL_Surface *Image;
SDL_Rect rScreen;
SDL_Rect rBuffer;
#define PI 3.1415926535f
const int UPDATE_INTERVAL = 30;
static int direct = 1;//1:右,2:左,3:上,4:下 默认向右
static Uint32 timeCount = 0;
/
Uint32 timeLeft(void);
void init(void);
void shutDown(void);
void loadBMP(void);
void loadBack(void);
void loadAnimal(void);
void OnKeyDown(int key);
void render(int direct);
/
void Tunnel_Timer(){}
double Tunnel_GetTime()
{
return SDL_GetTicks()*1.0/1000;
}
/
/
int main(int argc, char *argv[])
{
Tunnel_Timer();
init();
loadBMP();
loadBack();
loadAnimal();
bool bDone = false;
while( bDone == false )
{
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
bDone = true;
if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_ESCAPE )
bDone = true;
OnKeyDown(event.key.keysym.sym);
}
}
render(direct);
}
shutDown();
return 0;
}
void OnKeyDown(int key)
{
switch(key)
{
case SDLK_a:
break;
case SDLK_s:
break;
case SDLK_d:
break;
case SDLK_w:
break;
}
}
//
void init( void )
{
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s/n", SDL_GetError() );
exit(1);
}
atexit( SDL_Quit );
g_screenSurface = SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE );// | SDL_DOUBLEBUF );
if( g_screenSurface == NULL )
{
printf( "Unable to set 800x600 video: %s/n", SDL_GetError() );
exit(1);
}
/*
bBuffer = SDL_CreateRGBSurface( SDL_HWSURFACE,
256,//g_screenSurface->w,
256,//g_screenSurface->h,
g_screenSurface->format->BitsPerPixel,
g_screenSurface->format->Rmask,
g_screenSurface->format->Gmask,
g_screenSurface->format->Bmask,
g_screenSurface->format->Amask);
Image = SDL_LoadBMP( "tunnel_map.bmp" );
if (!bBuffer || !Image)
{
printf("Error: I can't load or create bmp !!!/n/n");
}
Image = SDL_ConvertSurface(Image, g_screenSurface->format, SDL_HWSURFACE);
rBuffer.x = 0;
rBuffer.y = 0;
rBuffer.w = bBuffer->w;
rBuffer.h = bBuffer->h;
SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
SDL_ShowCursor( SDL_DISABLE );
Tunnel.Set( 320, 240 ); // Dimension du tunnel
Tunnel.Precalc( 16 ); // Diametre du tunnel
*/
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
}
//
//-----------------------------------------------------------------------------
void loadBMP( void )
{
// Load the BMP file into a surface
g_donutSurface = SDL_LoadBMP( "donut.bmp" );
if( g_donutSurface == NULL )
{
fprintf(stderr, "Couldn't load /"donut.bmp/"", SDL_GetError());
return;
}
SDL_SetColorKey( g_donutSurface, SDL_SRCCOLORKEY,
SDL_MapRGB( g_donutSurface->format, 0, 0, 0) );
}
void loadBack( void )
{
g_backface = SDL_LoadBMP("back2.bmp");
if( g_backface == NULL )
{
fprintf(stderr, "Couldn't load /"back2.bmp/"", SDL_GetError());
return;
}
SDL_SetColorKey( g_backface, SDL_SRCCOLORKEY,
SDL_MapRGB( g_backface->format, 0, 0, 0) );
}
void loadAnimal( void )
{
g_test = SDL_LoadBMP("back2.bmp");
if( g_test == NULL )
{
fprintf(stderr, "Couldn't load /"back2.bmp/"", SDL_GetError());
return;
}
SDL_SetColorKey( g_test, SDL_SRCCOLORKEY,
SDL_MapRGB( g_test->format, 0, 0, 0) );
}
//
Uint32 timeLeft( void )
{
static Uint32 timeToNextUpdate = 0;
Uint32 currentTime;
currentTime = SDL_GetTicks();
if( timeToNextUpdate <= currentTime )
{
timeToNextUpdate = currentTime + UPDATE_INTERVAL;
return 0;
}
//进行时间控制
timeCount = timeCount + 10;
return( timeToNextUpdate - currentTime );
}
void shutDown( void )
{
//Tunnel.Free();
SDL_FreeSurface( g_donutSurface );
SDL_FreeSurface( g_screenSurface );
SDL_FreeSurface( g_backface );
SDL_FreeSurface(g_test);
}
void render(int iDirect)
{
SDL_Delay( timeLeft() );
//SDL_FillRect:刷主页面这里rect为null,将整个g_screenSurface背景色置成0,0,0,黑色
SDL_FillRect( g_screenSurface, NULL, SDL_MapRGB( g_screenSurface->format, 0,0,0));
//
// Blit the bitmap back surface to the main surface
//
SDL_BlitSurface( g_backface, NULL, g_screenSurface, NULL );
//
// Blit the bitmap animal surface to the main surface
//
/*
SDL_Rect srcRectAnimal;
SDL_Rect destRectAnimal;
srcRectAnimal.x = 200;
srcRectAnimal.y = 300;
srcRectAnimal.w = 100;
srcRectAnimal.h = 50;
destRectAnimal.x = 400;
destRectAnimal.y = 100;
destRectAnimal.w = 100;
destRectAnimal.h = 50;
SDL_BlitSurface( g_test, &srcRectAnimal,
g_screenSurface, &destRectAnimal );
*/
//SDL_Delay(1000);
//SDL_UpdateRects( g_screenSurface, 1, &destRectAnimal );
//
// Blit the bitmap surface to the main surface
//
/*
static int nPosition = 0;
static int nFrame = 0;
SDL_Rect srcRect;
SDL_Rect destRect;
// Build a source SDL_Rect which will copy only a small portion of the texture.
srcRect.x = (( nFrame % 5 ) * 64);//从原始的大组图中切割小图块用来写入主页面
srcRect.y = (( nFrame / 5 ) * 64);
srcRect.w = 64;
srcRect.h = 64;
destRect.x = nPosition;
destRect.y = 500;
destRect.w = 64;
destRect.h = 64;
SDL_BlitSurface( g_donutSurface, &srcRect, g_screenSurface, &destRect );
// Update the changed portion of the screen
SDL_UpdateRects( g_screenSurface, 1, &destRect );
//
// Increment the sprite's frame number. Our sprite's animation sequence
// consists of 30 frames (0-29).
//
++nFrame;//一共30张小图,切割完了就从头再切
if( nFrame > 29 )
nFrame = 0;
//
// Slowly move our sprite across the screen. This demonstrates how to use
// a SDL_Rect destination rect with the SDL_BlitSurface method to place a
// sprite on the main screen surface.
//
++nPosition;
if( nPosition > 800 )
nPosition = 0;
*/
SDL_Flip( g_screenSurface );
/
/*
float fTime = Tunnel_GetTime();
SDL_LockSurface(bBuffer);
SDL_LockSurface(Image);
// et on le dessine normalement !
// Tunnel.Draw(bBuffer, Image, fTime*100, fTime*100);
// ou un peu plus delire
Tunnel.Draw(bBuffer, Image, 180*sin(fTime), fTime*100);
SDL_UnlockSurface(bBuffer);
SDL_UnlockSurface(Image);
SDL_BlitSurface( bBuffer, NULL, g_screenSurface, &rBuffer );
SDL_UpdateRect( g_screenSurface, 0, 0, 0, 0 );
*/
///
}