//注意游戏开始界面的按钮替换
//时间使用
//转换屏幕
#i nclude <stdio.h> #i nclude <stdlib.h> #i nclude <string.h> #i nclude <SDL.h> #i nclude <windows.h>
#i nclude "Q3DMath.h" #i nclude "QuickCG.h"
SDL_Surface *pScreen = NULL;//屏幕表面
int btnState;//开始时按钮状态
int gameState;//游戏状态
void display_bmp(char *file_name); void EventCenter(SDL_KeyboardEvent *key); void PrintKeyInfo( SDL_KeyboardEvent *key ); void ClearScreen(SDL_Surface *surface); void display_bmp1(char *file_name,int x,int y); void display_bmp2(char *file_name,SDL_Rect sourceRect,int x,int y); void DrawSprite(); Uint32 displaySprite(Uint32 interval, void *param);
struct sprite { char *name; int direct; };
sprite marry;
int main (int argc, char *argv[])//必须要加参数
{ char *msg; int done;
btnState=0;//开始按钮
gameState=0;//游戏处在开始选单
marry.name="marry"; marry.direct=4;
/* Initialize SDL */ if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)//初始化SDL
{ sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ()); MessageBox (0, msg, "Error", MB_ICONHAND); free (msg); exit (1); } atexit (SDL_Quit);//释放
/* Set 640x480 16-bits video mode */ //设置视频模式640*480
pScreen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); if (pScreen == NULL) { sprintf (msg, "Couldn't set 640x480x16 video mode: %s\n", SDL_GetError ()); MessageBox (0, msg, "Error", MB_ICONHAND); free (msg); exit (2); } //设置窗口标题
SDL_WM_SetCaption ("Game Example", NULL);
//SDL_CreateCursor(0,1,32,32,5,5);
display_bmp("123.bmp");
display_bmp1("btn3.bmp",230,225);
display_bmp1("btn2.bmp",230,270); //事件循环
done = 0; while (!done) { SDL_Event event;
/* Check for events */ //检测事件
while (SDL_PollEvent (&event)) { switch (event.type) { case SDL_KEYDOWN: { //MessageBox(NULL,"FDFDFDS","SDFDSFDS",0);
} break; case SDL_KEYUP: { EventCenter(&event.key); } break; case SDL_QUIT: done = 1; break; default: break; } }
/* Draw to screen */ //绘制屏幕
//draw ();//绘制不同色彩的矩形
}
return 0; }
void display_bmp(char *file_name) { SDL_Surface *image;
/* Load the BMP file into a surface */ //加载位图至屏幕表面
image = SDL_LoadBMP(file_name); if (image == NULL) { fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError()); return; }
/* * Palettized screen modes will have a default palette (a standard * 8*8*4 colour cube), but if the image is palettized as well we can * use that palette for a nicer colour matching */ //如果图像调色板与屏幕调色板一致的话,设置屏幕的一部分色彩为图像的色彩
if (image->format->palette && pScreen->format->palette) { SDL_SetColors(pScreen, image->format->palette->colors, 0, image->format->palette->ncolors); }
/* Blit onto the pScreen surface */ //迅速传输image表面至于pScreen表面
if(SDL_BlitSurface(image, NULL, pScreen, NULL) < 0) fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
//更新指定区域
SDL_UpdateRect(pScreen, 0, 0, image->w, image->h);
/* Free the allocated BMP surface */ //清除所分配的bmp表面
SDL_FreeSurface(image); //翻页
SDL_Flip(pScreen); }
void display_bmp1(char *file_name,int x,int y) { SDL_Surface *image; SDL_Rect destRect;
/* Load the BMP file into a surface */ //加载位图至屏幕表面
image = SDL_LoadBMP(file_name); if (image == NULL) { fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError()); return; }
/* * Palettized pScreen modes will have a default palette (a standard * 8*8*4 colour cube), but if the image is palettized as well we can * use that palette for a nicer colour matching */ //如果图像调色板与屏幕调色板一致的话,设置屏幕的一部分色彩为图像的色彩
if (image->format->palette && pScreen->format->palette) { SDL_SetColors(pScreen, image->format->palette->colors, 0, image->format->palette->ncolors); }
/* Blit onto the pScreen surface */ //迅速传输image表面至于pScreen表面
destRect.x=x; destRect.y=y; if(SDL_BlitSurface(image, NULL, pScreen, &destRect) < 0) fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
//更新指定区域
SDL_UpdateRect(pScreen, 0, 0, image->w, image->h);
/* Free the allocated BMP surface */ //清除所分配的bmp表面
SDL_FreeSurface(image); //翻页
SDL_Flip(pScreen); }
void display_bmp2(char *file_name,SDL_Rect sourceRect,int x,int y) { SDL_Surface *image; SDL_Rect destRect;
/* Load the BMP file into a surface */ //加载位图至屏幕表面
image = SDL_LoadBMP(file_name); if (image == NULL) { fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError()); return; }
//如果图像调色板与屏幕调色板一致的话,设置屏幕的一部分色彩为图像的色彩
if (image->format->palette && pScreen->format->palette) { SDL_SetColors(pScreen, image->format->palette->colors, 0, image->format->palette->ncolors); }
//迅速传输image表面至于pScreen表面
destRect.x=x; destRect.y=y; if(SDL_BlitSurface(image, &sourceRect, pScreen, &destRect) < 0) fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
//更新指定区域
SDL_UpdateRect(pScreen, 0, 0, image->w, image->h);
//清除所分配的bmp表面
SDL_FreeSurface(image); //翻页
SDL_Flip(pScreen); }
void EventCenter(SDL_KeyboardEvent *key) { switch (key->keysym.sym) { case SDLK_RETURN: { if(gameState==0) { if(btnState==0) { ClearScreen(pScreen); gameState=1;//进入游戏
} else { exit(2); } } } break; case SDLK_ESCAPE: { exit(1); } break; case SDLK_UP: { if(btnState==1) { btnState=0; display_bmp1("btn3.bmp",230,225); display_bmp1("btn2.bmp",230,270); } } break; case SDLK_DOWN: {
if(btnState==0) { btnState=1; display_bmp1("btn1.bmp",230,225); display_bmp1("btn4.bmp",230,270); }
} break; case SDLK_LEFT: { marry.direct=3; DrawSprite(); } break;; case SDLK_RIGHT: { marry.direct=4; DrawSprite(); } break; } }
void PrintKeyInfo( SDL_KeyboardEvent *key ) { /* Is it a release or a press? */ if( key->type == SDL_KEYUP ) printf( "Release:- " ); else printf( "Press:- " );
/* Print the hardware scancode first */ printf( "Scancode: 0x%02X", key->keysym.scancode ); /* Print the name of the key */ printf( ", Name: %s", SDL_GetKeyName( key->keysym.sym ) ); /* We want to print the unicode info, but we need to make */ /* sure its a press event first (remember, release events */ /* don't have unicode info */ if( key->type == SDL_KEYDOWN ){ /* If the Unicode value is less than 0x80 then the */ /* unicode value can be used to get a printable */ /* representation of the key, using (char)unicode. */ printf(", Unicode: " ); if( key->keysym.unicode < 0x80 && key->keysym.unicode > 0 ){ printf( "%c (0x%04X)", (char)key->keysym.unicode, key->keysym.unicode ); } else{ printf( "? (0x%04X)", key->keysym.unicode ); } } printf( "\n" ); /* Print modifier info */ //PrintModifiers( key->keysym.mod );
}
void ClearScreen(SDL_Surface *surface) { Uint32 color; if(SDL_MUSTLOCK(surface)) { SDL_LockSurface(surface); }
color=SDL_MapRGB(surface->format,0,0,0); SDL_FillRect(surface,NULL,color);
if(SDL_MUSTLOCK(surface)) { SDL_UnlockSurface(surface); }
SDL_Flip(surface); }
void setpixel(SDL_Surface *surface, int x, int y, Uint8 r, Uint8 g, Uint8 b) { Uint8 *ubuff8; Uint16 *ubuff16; Uint32 *ubuff32; Uint32 color; char c1, c2, c3; /* Lock the screen, if needed */ if(SDL_MUSTLOCK(surface)) { if(SDL_LockSurface(surface) < 0) return; } /* Get the color */ color = SDL_MapRGB( surface->format, r, g, b ); /* How we draw the pixel depends on the bitdepth */ switch(surface->format->BytesPerPixel) { case 1: ubuff8 = (Uint8*) surface->pixels; ubuff8 += (y * surface->pitch) + x; *ubuff8 = (Uint8) color; break;
case 2: ubuff8 = (Uint8*) surface->pixels; ubuff8 += (y * surface->pitch) + (x*2); ubuff16 = (Uint16*) ubuff8; *ubuff16 = (Uint16) color; break;
case 3: ubuff8 = (Uint8*) surface->pixels; ubuff8 += (y * surface->pitch) + (x*3);
if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { c1 = (color & 0xFF0000) >> 16; c2 = (color & 0x00FF00) >> 8; c3 = (color & 0x0000FF); } else { c3 = (color & 0xFF0000) >> 16; c2 = (color & 0x00FF00) >> 8; c1 = (color & 0x0000FF); }
ubuff8[0] = c3; ubuff8[1] = c2; ubuff8[2] = c1; break; case 4: ubuff8 = (Uint8*) surface->pixels; ubuff8 += (y*surface->pitch) + (x*4); ubuff32 = (Uint32*)ubuff8; *ubuff32 = color; break; default: fprintf(stderr, "Error: Unknown bitdepth!\n"); } /* Unlock the screen if needed */ if(SDL_MUSTLOCK(surface)) { SDL_UnlockSurface(surface); } }
void DrawPicture(SDL_Surface* surface) { // Draw all the colors...
int r = 255, g = 0, b = 255; int x, y; for(x = 0; x < surface->w; x++ ) { for( y = 0; y < surface->h; y++ ) { setpixel(surface, x, y, x, y, 0); } } SDL_Flip(surface); }
void DrawSprite() { SDL_TimerID timeID=SDL_AddTimer(500,displaySprite,NULL); }
Uint32 displaySprite(Uint32 interval, void *param) {
SDL_Rect spriteRect; Uint32 tranColor; tranColor=SDL_MapRGB(pScreen->format,255,0,255); SDL_SetColorKey(pScreen,SDL_SRCCOLORKEY,tranColor); if(spriteRect.x<96) { spriteRect.x=+32; } else { spriteRect.x=0; } spriteRect.y=96; spriteRect.w=32; spriteRect.h=48; display_bmp2("char1.bmp",spriteRect,10,10); return 1; }
|