老實說,官方文件不怎麼完整。加上我英文看膩了(應該是說看到吐了)
好在有個很棒的簡中文件,只要你搞定大陸用語就好了。就算真的看不懂,他提供的原始碼註解都是英文的,看不懂的也看懂了
SDL入門教程-再別流年的技術實驗室
公告版位
目前分類:SDL程式開發 (2)
- Sep 13 Sat 2008 14:14
被SDL官方文件弄死...
- Sep 10 Wed 2008 20:29
使用SDL library建立基本視窗(SDL tutorial真是OOXX)
後面的括號是我的心聲阿,何也?我建立好視窗後一執行,才發現他沒辦法關掉...我認為這應該在建立視窗那裡就提出來
所以以下就把建立基本視窗的程式碼列上,免的又有人找老半天...
相信我,你絕對得讀英文。SDL的中文文件真是有夠少的(另外就是大陸用語不同,到時候誤解大了就你慘兮兮),真的厭惡英文的話請到google查SDL教程吧
這部份的原始碼是從The Simple Directmedia Layer documentation取得,依照其授權規定,他是在GPL的管轄下
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#define TICK_INTERVAL 20
/*In a game loop you will want some kind of delay,
to maintain a constant speed and to ensure your program doesn't always use 99% of the processor.
This is a useful construct for maintaining a target framerate,
rather than having a constant sleep time.
Here the target interval is 30ms which equates to 1000/30 = 33 frames per second.*/
static Uint32 next_time;
Uint32 time_left(void)
{
Uint32 now;
now = SDL_GetTicks();
if(next_time <= now)
return 0;
else
return next_time - now;
}
/*On win32 it can be useful to call SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS) at the start of your program.
This will push the resolution of SDL_Delay into the millisecond range.
Normally, it has a resolution of about 10ms.*/
int main(int argc, char *argv[])
{
SDL_Surface *screen = NULL;
SDL_Event event;
//Initialize SDL VIDEO subsystem
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Couldn't Initialize SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);//Clean up on exit
//Initialize the display in a 640x480 8-bit palettized mode, and requesting a software surface(SDL_SWSURFACE)
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
/*If you have a preference for a certain pixel depth but will accept any other,
use SDL_SetVideoMode with SDL_ANYFORMAT as below.
You can also use SDL_VideoModeOK to find the native video mode that is closest to the mode you request. */
if (screen == NULL )
{
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
//main part
next_time = SDL_GetTicks() + TICK_INTERVAL;
while(game_run)
{
//update game start
SDL_Delay(time_left());
next_time += TICK_INTERVAL;
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
printf("Program Quiting...\n");
exit(0);
}
}
SDL_Quit();
return 0;
}