Ejemplo n.º 1
0
	bool SDLApplication::init(int /*argc*/, char ** /*argv*/)
	{
		if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
			Trace::error("Could not initialize SDL: %s\n", SDL_GetError());
			SDL_Quit();
			return false;
		}

		const SDL_VideoInfo * videoInfo = SDL_GetVideoInfo();
		if (videoInfo == NULL) {
			Trace::error("Could not get video information: %s\n", SDL_GetError());
			SDL_Quit();
			return false;
		}

		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

		m_surface = SDL_SetVideoMode(800, 600, videoInfo->vfmt->BitsPerPixel, SDL_OPENGL | SDL_RESIZABLE);
		if (!m_surface) {
			Trace::error("Could not set video mode: %s\n", SDL_GetError());
			SDL_Quit();
			return false;
		}

		SDL_WM_SetCaption(m_title, m_title);

		window_resized(m_surface->w, m_surface->h);

		return true;
	}
Ejemplo n.º 2
0
static void
leave_full_screen(void)
{
	if(amFullScreen) {
		EndFullScreen(fullScreenRestore, 0);
		theWindow = oldWindow;
		ShowWindow(theWindow);
		amFullScreen = 0;
		window_resized();
		Rectangle rect =  { { 0, 0 }, { bounds.size.width, bounds.size.height } };
		wmtrack(0, rect.max.x, rect.max.y, 0);
		drawqlock();
		flushmemscreen(rect);
		drawqunlock();
	}
}
Ejemplo n.º 3
0
static void
full_screen(void)
{
	if(!amFullScreen) {
		oldWindow = theWindow;
		HideWindow(theWindow);
		GDHandle device;
		GetWindowGreatestAreaDevice(theWindow, kWindowTitleBarRgn, &device, NULL);
		BeginFullScreen(&fullScreenRestore, device, 0, 0, &theWindow, 0, 0);
		amFullScreen = 1;
		window_resized();
		Rectangle rect =  { { 0, 0 }, { bounds.size.width, bounds.size.height } };
		wmtrack(0, rect.max.x, rect.max.y, 0);
		drawqlock();
		flushmemscreen(rect);
		drawqunlock();
	} else
		leave_full_screen();
}
Ejemplo n.º 4
0
int main(int argc, char **argv) {
	int return_status = 1;

	#define MAIN_SDL_CHECK(expression, error_prefix) { \
		if (!(expression)) { \
			log_error(error_prefix, SDL_GetError()); \
			goto exit; \
		} \
	}

	MAIN_SDL_CHECK(SDL_Init(SDL_INIT_VIDEO) == 0, "SDL_Init");
	if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
		log_error("IMG_Init", IMG_GetError());
		goto exit;
	}

	MAIN_SDL_CHECK(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"), "SDL_SetHint SDL_HINT_RENDER_SCALE_QUALITY");

	startup_info_t info = startup(argc, argv);
	if (!info.success)
		goto exit;

	net_mode_t net_mode = info.net_mode;
	network = info.network;

	char wtitle[256];
	if (net_mode == NET_SERVER) {
		snprintf(wtitle, sizeof(wtitle), "NetCheckers - server (%s)", info.port);
	} else {
		snprintf(wtitle, sizeof(wtitle), "NetCheckers - client (%s:%s)", info.host, info.port);
	}
	window = SDL_CreateWindow(
		wtitle,
#if 1
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
#else
		(net_mode == NET_SERVER ? 10 : window_width + 20), SDL_WINDOWPOS_CENTERED,
#endif
		window_width, window_height,
		SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
	);
	MAIN_SDL_CHECK(window, "SDL_CreateWindow");

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	MAIN_SDL_CHECK(renderer, "SDL_CreateRenderer");
	MAIN_SDL_CHECK(SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) == 0, "SDL_SetRenderDrawBlendMode SDL_BLENDMODE_BLEND");

	char *error = 0;
	char path[1024];
	#define LOAD_TEXTURE_CHECKED(var, file) { \
		sprintf(path, "%s/" file, info.assets_path); \
		var = load_png_texture(path, &error); \
		if (error) { \
			log_error("load_png_texture " file, error); \
			goto exit; \
		} \
	}
	LOAD_TEXTURE_CHECKED(tex.textures.board, "board.png");
	LOAD_TEXTURE_CHECKED(tex.textures.red_piece, "piece_red.png");
	LOAD_TEXTURE_CHECKED(tex.textures.red_piece_king, "piece_red_king.png");
	LOAD_TEXTURE_CHECKED(tex.textures.white_piece, "piece_white.png");
	LOAD_TEXTURE_CHECKED(tex.textures.white_piece_king, "piece_white_king.png");
	LOAD_TEXTURE_CHECKED(tex.textures.highlight, "highlight.png");
	LOAD_TEXTURE_CHECKED(tex.textures.player_turn, "player_turn.png");
	LOAD_TEXTURE_CHECKED(tex.textures.opponent_turn, "opponent_turn.png");
	LOAD_TEXTURE_CHECKED(tex.textures.victory, "victory.png");
	LOAD_TEXTURE_CHECKED(tex.textures.defeat, "defeat.png");

	window_resized(window_width, window_height);

// Put the pieces on the board
#if 0
	// Game over testing
	for (int i = 0; i < 24; i++) {
		piece_t *piece = pieces + i;
		piece->color = (i >= 12) ? PIECE_BLACK : PIECE_WHITE;
		piece->captured = true;
	}
	pieces[0].captured = false;
	pieces[0].king = true;
	pieces[0].pos = cell_pos(1, 3);
	board[1][3] = &pieces[0];

	pieces[12].captured = false;
	pieces[12].king = true;
	pieces[12].pos = cell_pos(5, 3);
	board[5][3] = &pieces[12];
#else
	int fill_row = 0;
	int fill_col = 0;

	for (int i = 0; i < 12; i++) {
		piece_t *piece = pieces + i;
		piece->color = PIECE_WHITE;
		piece->pos = cell_pos(fill_row, fill_col);
		board[fill_row][fill_col] = piece;
		advance_board_row_col(&fill_row, &fill_col);
	}

	fill_row = 5;
	fill_col = 1;
	for (int i = 12; i < 24; i++) {
		piece_t *piece = pieces + i;
		piece->color = PIECE_BLACK;
		piece->pos = cell_pos(fill_row, fill_col);
		board[fill_row][fill_col] = piece;
		advance_board_row_col(&fill_row, &fill_col);
	}
#endif

	game_over = false;
	current_turn = PIECE_BLACK;
	local_color = (net_mode == NET_SERVER) ? PIECE_BLACK : PIECE_WHITE;

	bool running = true;
	int last_time = SDL_GetTicks();
	while (running) {
		int current_time = SDL_GetTicks();
		int ellapsed_ms = current_time - last_time;
		last_time = current_time;
		float delta_time = (float)ellapsed_ms / 1000.0f;

		SDL_Event event = {0};
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
				case SDL_QUIT: {
					running = false;
				} break;
				case SDL_WINDOWEVENT: {
					if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
						window_resized(event.window.data1, event.window.data2);
					}
				} break;
				case SDL_MOUSEBUTTONDOWN: {
					#if 1
					if (event.button.state == SDL_PRESSED && event.button.button == SDL_BUTTON_RIGHT) {
						message_t net_msg = {0};
						net_msg.move_piece = cell_pos(5, 1);
						net_msg.move_target = cell_pos(4, 0);
						net_send_message(network, &net_msg);
					}
					#endif
					if (event.button.state == SDL_PRESSED && event.button.button == SDL_BUTTON_LEFT) {
						if (!game_over && !animating_piece && current_turn == local_color) {
							int click_x = event.button.x * dpi_rate;
							int click_y = event.button.y * dpi_rate;
							if (rect_includes(&board_rect, click_x, click_y)) {
								cell_pos_t clicked_cell = point_to_cell(click_x, click_y);
								piece_t *clicked_piece = board[clicked_cell.row][clicked_cell.col];
								if (clicked_piece && clicked_piece->color == current_turn) {
									piece_moves_t moves = find_valid_moves(clicked_piece);
									if (moves.count) {
										selected_piece = clicked_piece;
										available_moves = moves;
									}
								} else if (selected_piece) {
									cell_pos_t from_cell = selected_piece->pos;

									move_result_t res = perform_move(selected_piece, clicked_cell);
									if (res != MOVE_INVALID) {
										if (res == MOVE_END_TURN) {
											selected_piece = 0;
										} else {
											available_moves = find_valid_moves(selected_piece);
										}

										message_t net_msg = {0};
										net_msg.move_piece = from_cell;
										net_msg.move_target = clicked_cell;
										if (!net_send_message(network, &net_msg)) {
											int err = SDL_ShowSimpleMessageBox(
												SDL_MESSAGEBOX_ERROR,
												"Erro - Falha de comunicação",
												"Falha ao enviar movimento para o adversário.",
												window
											);
											if (err) {
												log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError());
											}
											goto exit;
										}
									}
								}
							}
						}
					}
				} break;
			}
		}

		if (net_get_state(network) != NET_RUNNING) {
			int err = SDL_ShowSimpleMessageBox(
				SDL_MESSAGEBOX_ERROR,
				"Erro - Conexão Interrompida",
				"A conexão foi interrompida pelo adversário",
				window
			);
			if (err) {
				log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError());
			}
			goto exit;
		}

		message_t net_msg;
		if (net_poll_message(network, &net_msg)) {
			bool valid_move = false;

			piece_t *piece = board[net_msg.move_piece.row][net_msg.move_piece.col];
			if (piece && current_turn != local_color && piece->color != local_color) {
				move_result_t res = perform_move(piece, net_msg.move_target);
				if (res != MOVE_INVALID)
					valid_move = true;
			}

			if (!valid_move) {
				int err = SDL_ShowSimpleMessageBox(
					SDL_MESSAGEBOX_ERROR,
					"Erro - Movimento inválido",
					"Seu adversário enviou um movimento inválido.",
					window
				);
				if (err) {
					log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError());
				}
				goto exit;
			}
		}

		render(delta_time);
	}

	return_status = 0;
exit:
	if (network)
		net_destroy(network);
	for (int i = 0; i < ARRAY_SIZE(tex.array); i++) {
		if (tex.array[i])
			SDL_DestroyTexture(tex.array[i]);
	}
	if (renderer)
		SDL_DestroyRenderer(renderer);
	if (window)
		SDL_DestroyWindow(window);
	IMG_Quit();
	SDL_Quit();
	return return_status;
}
Ejemplo n.º 5
0
//default window event handler
static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
{
    OSStatus result = noErr;
	uint32_t d_width;
	uint32_t d_height;
	UInt32 class = GetEventClass (event);
	UInt32 kind = GetEventKind (event); 

	result = CallNextEventHandler(nextHandler, event);
	
	aspect(&d_width,&d_height,A_NOZOOM);

	if(class == kEventClassCommand)
	{
		HICommand theHICommand;
		GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
		
		switch ( theHICommand.commandID )
		{
			case kHICommandQuit:
				mplayer_put_key(KEY_CLOSE_WIN);
				break;
				
			case kHalfScreenCmd:
					if(vo_quartz_fs)
					{
						vo_fs = (!(vo_fs)); window_fullscreen();
					}
						
					SizeWindow(theWindow, (d_width/2), ((d_width/movie_aspect)/2), 1);
					window_resized();
				break;

			case kNormalScreenCmd:
					if(vo_quartz_fs)
					{
						vo_fs = (!(vo_fs)); window_fullscreen();
					}
						
					SizeWindow(theWindow, d_width, (d_width/movie_aspect), 1);
					window_resized();
				break;

			case kDoubleScreenCmd:
					if(vo_quartz_fs)
					{
						vo_fs = (!(vo_fs)); window_fullscreen();
					}
						
					SizeWindow(theWindow, (d_width*2), ((d_width/movie_aspect)*2), 1);
					window_resized();
				break;

			case kFullScreenCmd:
				vo_fs = (!(vo_fs)); window_fullscreen();
				break;

			case kKeepAspectCmd:
				vo_keepaspect = (!(vo_keepaspect));
				CheckMenuItem (aspectMenu, 1, vo_keepaspect);
				window_resized();
				break;
				
			case kAspectOrgCmd:
				movie_aspect = old_movie_aspect;
				if(!vo_quartz_fs)
				{
					SizeWindow(theWindow, dstRect.right, (dstRect.right/movie_aspect),1);
				}
				window_resized();
				break;
				
			case kAspectFullCmd:
				movie_aspect = 4.0f/3.0f;
				if(!vo_quartz_fs)
				{
					SizeWindow(theWindow, dstRect.right, (dstRect.right/movie_aspect),1);
				}
				window_resized();
				break;
				
			case kAspectWideCmd:
				movie_aspect = 16.0f/9.0f;
				if(!vo_quartz_fs)
				{
					SizeWindow(theWindow, dstRect.right, (dstRect.right/movie_aspect),1);
				}
				window_resized();
				break;
				
			case kPanScanCmd:
				vo_panscan = (!(vo_panscan));
				CheckMenuItem (aspectMenu, 2, vo_panscan);
				window_panscan();
				window_resized();
				break;
			
			default:
				result = eventNotHandledErr;
				break;
		}
	}
	else if(class == kEventClassWindow)
Ejemplo n.º 6
0
	int SDLApplication::run()
	{
		bool quit = false;
		SDL_Event event;

		m_totalTime = SDL_GetTicks();

		while (!quit)
		{
			while (SDL_PollEvent(&event))
			{
				switch (event.type)
				{
				case SDL_KEYDOWN:
					if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)
						quit = true;
					else if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_w)
						keyDown(KEY_MOVE_FORWARD);
					else if (event.key.keysym.sym == SDLK_DOWN || event.key.keysym.sym == SDLK_s)
						keyDown(KEY_MOVE_BACKWARD);
					else if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_a)
						keyDown(KEY_MOVE_LEFT);
					else if (event.key.keysym.sym == SDLK_RIGHT || event.key.keysym.sym == SDLK_d)
						keyDown(KEY_MOVE_RIGHT);
					else if (event.key.keysym.sym == SDLK_SPACE)
						keyDown(KEY_CONTINUE);
					break;
				case SDL_KEYUP:
					if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)
						quit = true;
					else if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_w)
						keyUp(KEY_MOVE_FORWARD);
					else if (event.key.keysym.sym == SDLK_DOWN || event.key.keysym.sym == SDLK_s)
						keyUp(KEY_MOVE_BACKWARD);
					else if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_a)
						keyUp(KEY_MOVE_LEFT);
					else if (event.key.keysym.sym == SDLK_RIGHT || event.key.keysym.sym == SDLK_d)
						keyUp(KEY_MOVE_RIGHT);
					else if (event.key.keysym.sym == SDLK_RETURN)
						keyUp(KEY_RESET_1);
					else if (event.key.keysym.sym == SDLK_c)
						keyUp(KEY_RESET_2);
					else if (event.key.keysym.sym == SDLK_h)
						keyUp(KEY_HELP);
					else if (event.key.keysym.sym == SDLK_SPACE)
						keyUp(KEY_CONTINUE);
					break;
				case SDL_MOUSEBUTTONDOWN:
					// this is to avoid a "jump" of the mouse delta after the first movement after a mouse click
					SDL_WarpMouse(m_width/2, m_height/2);
					break;
				case SDL_MOUSEMOTION:
					if (event.button.button == SDL_BUTTON_LEFT) {
						int middleX = int(m_width/2);
						int middleY = int(m_height/2);
						if (middleX != event.motion.x || middleY != event.motion.y)
						{
							mouse(KEY_MOUSE_LEFT, middleX - event.motion.x, middleY - event.motion.y);
							SDL_WarpMouse(middleX, middleY);
						}
					}

					break;
				case SDL_VIDEORESIZE:
					{
						Uint8 bpp = m_surface->format->BitsPerPixel;
						Uint32 flags = m_surface->flags;
						SDL_FreeSurface(m_surface);
						m_surface = SDL_SetVideoMode(event.resize.w, event.resize.h, bpp, flags);
						window_resized(event.resize.w, event.resize.h);
					}
					break;
				case SDL_QUIT:
					quit = true;
					break;
				}
			}

			int currentTime = SDL_GetTicks();
			float dt = float(currentTime - m_totalTime);

			if (!m_fixedTimeStep || dt >= m_targetElapsedTime * 1000) {
				m_totalTime = currentTime;
				if (dt > 0)
					update(dt);
			}

			draw();

			SDL_GL_SwapBuffers();
		}

		SDL_Quit();

		return 0;
	}
Ejemplo n.º 7
0
static void
winproc(void *a)
{
	MenuItemIndex index;

	winRect.left = 30;
	winRect.top = 60;
	winRect.bottom = (devRect.size.height * 0.75) + winRect.top;
	winRect.right = (devRect.size.width * 0.75) + winRect.left;

	ClearMenuBar();
	InitCursor();

	CreateStandardWindowMenu(0, &windMenu);
	InsertMenu(windMenu, 0);

	CreateNewMenu(1004, 0, &viewMenu);
	SetMenuTitleWithCFString(viewMenu, CFSTR("View"));
	AppendMenuItemTextWithCFString(viewMenu, CFSTR("Toggle Full Screen"), 0,
								kFullScreenCmd, &index);
	SetMenuItemCommandKey(viewMenu, index, FALSE, 'F');
	InsertMenu(viewMenu, GetMenuID(windMenu));

	DrawMenuBar();
	uint32_t windowAttrs = 0
				| kWindowCloseBoxAttribute
				| kWindowCollapseBoxAttribute
				| kWindowResizableAttribute
				| kWindowStandardHandlerAttribute
				| kWindowFullZoomAttribute
		;

	CreateNewWindow(kDocumentWindowClass, windowAttrs, &winRect, &theWindow);
	SetWindowTitleWithCFString(theWindow, CFSTR("Acme SAC"));

	if(PasteboardCreate(kPasteboardClipboard, &appleclip) != noErr)
		sysfatal("pasteboard create failed");

	const EventTypeSpec app_events[] = {
		{ kEventClassApplication, kEventAppQuit }
	};
	const EventTypeSpec commands[] = {
		{ kEventClassWindow, kEventWindowClosed },
		{ kEventClassWindow, kEventWindowBoundsChanged },
		{ kEventClassCommand, kEventCommandProcess }
	};
	const EventTypeSpec events[] = {
		{ kEventClassTextInput, kEventTextInputUpdateActiveInputArea },
		{ kEventClassTextInput, kEventTextInputUnicodeForKeyEvent },
		{ kEventClassTextInput, kEventTextInputOffsetToPos },
		{ kEventClassTextInput, kEventTextInputPosToOffset },
		{ kEventClassTextInput, kEventTextInputShowHideBottomWindow },
		{ kEventClassTextInput, kEventTextInputGetSelectedText },
		{ kEventClassTextInput, kEventTextInputUnicodeText },
		{ kEventClassTextInput, kEventTextInputFilterText },
		{ kEventClassKeyboard, kEventRawKeyDown },
		{ kEventClassKeyboard, kEventRawKeyModifiersChanged },
		{ kEventClassKeyboard, kEventRawKeyRepeat },
		{ kEventClassMouse, kEventMouseDown },
		{ kEventClassMouse, kEventMouseUp },
		{ kEventClassMouse, kEventMouseMoved },
		{ kEventClassMouse, kEventMouseDragged },
		{ kEventClassMouse, kEventMouseWheelMoved },
	};

	InstallApplicationEventHandler (
								NewEventHandlerUPP (ApplicationQuitEventHandler),
								GetEventTypeCount(app_events),
								app_events,
								NULL,
								NULL);

	InstallApplicationEventHandler (
								NewEventHandlerUPP (MainWindowEventHandler),
								GetEventTypeCount(events),
								events,
								NULL,
								NULL);
						
	InstallWindowEventHandler (
								theWindow,
								NewEventHandlerUPP (MainWindowCommandHandler),
								GetEventTypeCount(commands),
								commands,
								theWindow,
								NULL);

	ShowWindow(theWindow);
	ShowMenuBar();
	window_resized();
	Rectangle rect =  { { 0, 0 }, { bounds.size.width, bounds.size.height } };		
	wmtrack(0, rect.max.x, rect.max.y, 0);
	SelectWindow(theWindow);
	// Run the event loop
	readybit = 1;
	Wakeup(&rend);
	RunApplicationEventLoop();
}