Cocoa がフレームレートを抑制してるんじゃないか疑惑
OSX 上の SDL で全力で描画するときの fps が 59.xxx ぐらいだったので、これじゃ60fps出せないじゃないか! とショックを受けていたのですが、実は Cocoa? Quartz? 良くわかんないけど、その辺の何かがフレームレートを抑制してるんじゃなかろうか? と想像した。検証はまだ。適当にdelayを掛ければ確かめられるんじゃないかなあ、と思うのですが。
以下全力で画面更新するコード(delayなし。これで性能の限界のフレームレートが出て欲しいところなんですが……)。
eval_player で描画してるのは気のせい。
#include "SDL.h" SDL_Surface* screen; SDL_Surface* back; int width = 640; int height = 480; Uint16 mousex, mousey; void init(void) { SDL_Surface* temp; //Uint32 flags = SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_ANYFORMAT; Uint32 flags = SDL_DOUBLEBUF | SDL_ANYFORMAT; SDL_Rect r = {0, 0, width, height}; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(width, height, 32, flags); SDL_WM_SetCaption("kitten huffing", NULL); SDL_ShowCursor(SDL_DISABLE); temp = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); SDL_FillRect(temp, &r, 0xffffff); back = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); } int poll_event(void) { SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: return 0; break; case SDL_KEYDOWN: if (e.key.keysym.sym == SDLK_ESCAPE) { return 0; } break; case SDL_MOUSEMOTION: mousex = e.motion.x; mousey = e.motion.y; break; } } return 1; } void clear_screen(void) { SDL_BlitSurface(back, NULL, screen, NULL); } void eval_player(void) { const int width = 10; const int height = 10; SDL_Rect r = { mousex - width / 2, mousey - height / 2, width, height }; SDL_FillRect(screen, &r, 0); } void draw_player(void) { } void loop(void) { clear_screen(); eval_player(); draw_player(); SDL_Flip(screen); } void teardown(void) { SDL_Quit(); } int main(int ac, char** av) { Uint32 newtime, spf, pasttime; int frame; init(); while (poll_event()) { loop(); newtime = SDL_GetTicks(); spf += newtime - pasttime; pasttime = newtime; frame++; if (frame%100 == 0) { printf("%f\n", 100000.0 / spf); frame = spf = 0; } } teardown(); return 0; }
あれ?これ今みると時間の計り方が悪い気が。どうせなら100フレーム分まとめて測った方が無駄が無い。