예제 #1
0
void		SdlDisplay::sdlLauncher(bool multi)
{
    sdlInit(multi);
    createApple();
    while (start)
    {
        sdlEvent(multi);
        if ((SDL_GetTicks() - startticks) >= 80)
        {
            if (multi)
                P2ON = true;
            Run();
        }
        SDL_BlitSurface(back, NULL, screen, NULL);
        sdlBlitApple();
        sdlBlitMushroom();
        sdlPlaySongWhenEat();
        sdlBlitPlayer1();
        sdlBlitPlayer2(multi);
        sdlChangeEvent(multi);
        SDL_Flip(screen);
    }
    if (!freeall)
        sdlClose();
}
예제 #2
0
int GLApp::main(const std::vector<std::string>& args) {
	done = false;

//SDL init

	int sdlInitError = SDL_Init(getSDLInitFlags());
	if (sdlInitError) throw Common::Exception() << "SDL_Init failed with error code " << sdlInitError;

	Common::Finally sdlFinally([&](){ SDL_Quit(); });
		
	int width = 640;
	int height = 480;

	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	window = SDL_CreateWindow(getTitle(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
	if (!window) throw Common::Exception() << "failed to create window";
	Common::Finally sdlWindowFinally([&](){ SDL_DestroyWindow(window); });

	context = SDL_GL_CreateContext(window);
	if (!context) throw Common::Exception() << "failed to create GL context";
	Common::Finally sdlGlContextFinally([&](){ SDL_GL_DeleteContext(context); });

#if defined(PLATFORM_msvc)
	{
		GLenum err = glewInit();
		if (err != GLEW_OK) throw Common::Exception() << "GLEW failed to initialize with error " << glewGetErrorString(err); 
	}
#endif

	SDL_GL_SetSwapInterval(0);

	init();
	resize(width, height);

	SDL_Event event;
	do {
		while (SDL_PollEvent(&event) > 0) {
			switch (event.type) {
			case SDL_QUIT:
				done = true;
				break;
			case SDL_WINDOWEVENT:
				switch (event.window.event) {
				case SDL_WINDOWEVENT_RESIZED:
					width = event.window.data1;
					height = event.window.data2;
					resize(width, height);
					break;
				}
				break;
			case SDL_KEYDOWN:
#if PLATFORM_windows
					if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) != 0) {
						done = true;
					}
#endif
#if PLATFORM_osx
					if (event.key.keysym.sym == SDLK_q && (event.key.keysym.mod & KMOD_GUI) != 0) {
						done = true;
					}
#endif
				break;
			}
			sdlEvent(event);
		}

		update();

		if (swap) SDL_GL_SwapWindow(window);
	} while (!done);

	shutdown();

	return 0;
}