Skip to content

Commit

Permalink
REVIEWED: InitPlatform() organization and code-gardening
Browse files Browse the repository at this point in the history
  • Loading branch information
raysan5 committed Oct 23, 2023
1 parent 3ff6026 commit a0f0034
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 125 deletions.
33 changes: 20 additions & 13 deletions src/platforms/rcore_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,8 @@ void PollInputEvents(void)
// Initialize platform: graphics, inputs and more
int InitPlatform(void)
{
// Initialize display basic configuration
//----------------------------------------------------------------------------
CORE.Window.currentFbo.width = CORE.Window.screen.width;
CORE.Window.currentFbo.height = CORE.Window.screen.height;

Expand Down Expand Up @@ -545,27 +547,33 @@ int InitPlatform(void)
//AConfiguration_getKeyboard(platform.app->config);
//AConfiguration_getScreenSize(platform.app->config);
//AConfiguration_getScreenLong(platform.app->config);

// Set some default window flags
CORE.Window.flags &= ~FLAG_WINDOW_HIDDEN; // false
CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // false
CORE.Window.flags |= FLAG_WINDOW_MAXIMIZED; // true
CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // false
//----------------------------------------------------------------------------

// Initialize App command system
// NOTE: On APP_CMD_INIT_WINDOW -> InitGraphicsDevice(), InitTimer(), LoadFontDefault()...
//----------------------------------------------------------------------------
platform.app->onAppCmd = AndroidCommandCallback;
//----------------------------------------------------------------------------

// Initialize input events system
//----------------------------------------------------------------------------
platform.app->onInputEvent = AndroidInputCallback;
//----------------------------------------------------------------------------

// Initialize assets manager
InitAssetManager(platform.app->activity->assetManager, platform.app->activity->internalDataPath);
// Initialize storage system
//----------------------------------------------------------------------------
InitAssetManager(platform.app->activity->assetManager, platform.app->activity->internalDataPath); // Initialize assets manager

CORE.Storage.basePath = platform.app->activity->internalDataPath; // Define base path for storage
//----------------------------------------------------------------------------

// Initialize base path for storage
CORE.Storage.basePath = platform.app->activity->internalDataPath;

// Set some default window flags
CORE.Window.flags &= ~FLAG_WINDOW_HIDDEN; // false
CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // false
CORE.Window.flags |= FLAG_WINDOW_MAXIMIZED; // true
CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // false

TRACELOG(LOG_INFO, "PLATFORM: ANDROID: Application initialized successfully");
TRACELOG(LOG_INFO, "PLATFORM: ANDROID: Initialized successfully");

// Android ALooper_pollAll() variables
int pollResult = 0;
Expand Down Expand Up @@ -613,7 +621,6 @@ void ClosePlatform(void)
}
}


// Initialize display device and framebuffer
// NOTE: width and height represent the screen (framebuffer) desired size, not actual display size
// If width or height are 0, default display size will be used for framebuffer size
Expand Down
105 changes: 63 additions & 42 deletions src/platforms/rcore_desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,8 @@ int InitPlatform(void)
int result = glfwInit();
if (result == GLFW_FALSE) { TRACELOG(LOG_WARNING, "GLFW: Failed to initialize GLFW"); return -1; }

// Initialize graphic device: display/window and graphic context
//----------------------------------------------------------------------------
glfwDefaultWindowHints(); // Set default windows hints
//glfwWindowHint(GLFW_RED_BITS, 8); // Framebuffer red color component bits
//glfwWindowHint(GLFW_GREEN_BITS, 8); // Framebuffer green color component bits
Expand Down Expand Up @@ -1450,60 +1452,73 @@ int InitPlatform(void)
}

glfwMakeContextCurrent(platform.handle);
glfwSwapInterval(0); // No V-Sync by default

// Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
// NOTE: V-Sync can be enabled by graphic driver configuration, it doesn't need
// to be activated on web platforms since VSync is enforced there.
if (CORE.Window.flags & FLAG_VSYNC_HINT)
result = glfwGetError(NULL);

// Check context activation
if ((result != GLFW_NO_WINDOW_CONTEXT) && (result != GLFW_PLATFORM_ERROR))
{
// WARNING: It seems to hit a critical render path in Intel HD Graphics
glfwSwapInterval(1);
TRACELOG(LOG_INFO, "DISPLAY: Trying to enable VSYNC");
}
CORE.Window.ready = true;

int fbWidth = CORE.Window.screen.width;
int fbHeight = CORE.Window.screen.height;
glfwSwapInterval(0); // No V-Sync by default

if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
{
// NOTE: On APPLE platforms system should manage window/input scaling and also framebuffer scaling.
// Framebuffer scaling should be activated with: glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_TRUE);
#if !defined(__APPLE__)
glfwGetFramebufferSize(platform.handle, &fbWidth, &fbHeight);
// Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
// NOTE: V-Sync can be enabled by graphic driver configuration, it doesn't need
// to be activated on web platforms since VSync is enforced there.
if (CORE.Window.flags & FLAG_VSYNC_HINT)
{
// WARNING: It seems to hit a critical render path in Intel HD Graphics
glfwSwapInterval(1);
TRACELOG(LOG_INFO, "DISPLAY: Trying to enable VSYNC");
}

// Screen scaling matrix is required in case desired screen area is different from display area
CORE.Window.screenScale = MatrixScale((float)fbWidth/CORE.Window.screen.width, (float)fbHeight/CORE.Window.screen.height, 1.0f);
int fbWidth = CORE.Window.screen.width;
int fbHeight = CORE.Window.screen.height;

// Mouse input scaling for the new screen size
SetMouseScale((float)CORE.Window.screen.width/fbWidth, (float)CORE.Window.screen.height/fbHeight);
#endif
}
if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
{
// NOTE: On APPLE platforms system should manage window/input scaling and also framebuffer scaling.
// Framebuffer scaling should be activated with: glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_TRUE);
#if !defined(__APPLE__)
glfwGetFramebufferSize(platform.handle, &fbWidth, &fbHeight);

CORE.Window.render.width = fbWidth;
CORE.Window.render.height = fbHeight;
CORE.Window.currentFbo.width = fbWidth;
CORE.Window.currentFbo.height = fbHeight;
// Screen scaling matrix is required in case desired screen area is different from display area
CORE.Window.screenScale = MatrixScale((float)fbWidth/CORE.Window.screen.width, (float)fbHeight/CORE.Window.screen.height, 1.0f);

TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
// Mouse input scaling for the new screen size
SetMouseScale((float)CORE.Window.screen.width/fbWidth, (float)CORE.Window.screen.height/fbHeight);
#endif
}

// Load OpenGL extensions
// NOTE: GL procedures address loader is required to load extensions
rlLoadExtensions(glfwGetProcAddress);
CORE.Window.render.width = fbWidth;
CORE.Window.render.height = fbHeight;
CORE.Window.currentFbo.width = fbWidth;
CORE.Window.currentFbo.height = fbHeight;

TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
}
else
{
TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
return -1;
}

if ((CORE.Window.flags & FLAG_WINDOW_MINIMIZED) > 0) MinimizeWindow();

CORE.Window.ready = true; // TODO: Proper validation on windows/context creation

// If graphic device is no properly initialized, we end program
if (!CORE.Window.ready) { TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphic device"); return -1; }
else SetWindowPosition(GetMonitorWidth(GetCurrentMonitor())/2 - CORE.Window.screen.width/2, GetMonitorHeight(GetCurrentMonitor())/2 - CORE.Window.screen.height/2);

// Load OpenGL extensions
// NOTE: GL procedures address loader is required to load extensions
rlLoadExtensions(glfwGetProcAddress);
//----------------------------------------------------------------------------

// Initialize input events callbacks
//----------------------------------------------------------------------------
// Set window callback events
glfwSetWindowSizeCallback(platform.handle, WindowSizeCallback); // NOTE: Resizing not allowed by default!
glfwSetWindowMaximizeCallback(platform.handle, WindowMaximizeCallback);
Expand All @@ -1521,12 +1536,19 @@ int InitPlatform(void)
glfwSetJoystickCallback(JoystickCallback);

glfwSetInputMode(platform.handle, GLFW_LOCK_KEY_MODS, GLFW_TRUE); // Enable lock keys modifiers (CAPS, NUM)
//----------------------------------------------------------------------------

// Initialize hi-res timer
// Initialize timming system
//----------------------------------------------------------------------------
InitTimer();

// Initialize base path for storage
//----------------------------------------------------------------------------

// Initialize storage system
//----------------------------------------------------------------------------
CORE.Storage.basePath = GetWorkingDirectory();
//----------------------------------------------------------------------------

TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (GLFW): Initialized successfully");

return 0;
}
Expand All @@ -1542,7 +1564,6 @@ void ClosePlatform(void)
#endif
}


// GLFW3 Error Callback, runs on GLFW3 error
static void ErrorCallback(int error, const char *description)
{
Expand Down
34 changes: 25 additions & 9 deletions src/platforms/rcore_desktop_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ int InitPlatform(void)
int result = SDL_Init(SDL_INIT_EVERYTHING);
if (result < 0) { TRACELOG(LOG_WARNING, "SDL: Failed to initialize SDL"); return -1; }

// Initialize graphic device: display/window and graphic context
//----------------------------------------------------------------------------
unsigned int flags = 0;
flags |= SDL_WINDOW_SHOWN;
flags |= SDL_WINDOW_OPENGL;
Expand Down Expand Up @@ -1143,6 +1145,7 @@ int InitPlatform(void)
//if ((CORE.Window.flags & FLAG_FULLSCREEN_DESKTOP) > 0) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;

// NOTE: Some OpenGL context attributes must be set before window creation

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
Expand Down Expand Up @@ -1170,7 +1173,7 @@ int InitPlatform(void)
{
CORE.Window.ready = true;

SDL_DisplayMode displayMode;
SDL_DisplayMode displayMode = { 0 };
SDL_GetCurrentDisplayMode(GetCurrentMonitor(), &displayMode);

CORE.Window.display.width = displayMode.w;
Expand All @@ -1187,30 +1190,43 @@ int InitPlatform(void)
TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
}
else { TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphic device"); return -1; }
else
{
TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
return -1;
}

// Load OpenGL extensions
// NOTE: GL procedures address loader is required to load extensions
rlLoadExtensions(SDL_GL_GetProcAddress);
//----------------------------------------------------------------------------


// Init input gamepad
// Initialize input events system
//----------------------------------------------------------------------------
if (SDL_NumJoysticks() >= 1)
{
SDL_Joystick *gamepad = SDL_JoystickOpen(0);
//if (SDL_Joystick *gamepad == NULL) SDL_Log("WARNING: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
}
//----------------------------------------------------------------------------

// Initialize hi-res timer
//InitTimer();
// Initialize timming system
//----------------------------------------------------------------------------
// NOTE: No need to call InitTimer(), let SDL manage it internally
CORE.Time.previous = GetTime(); // Get time as double
//----------------------------------------------------------------------------

// Initialize base path for storage
CORE.Storage.basePath = GetWorkingDirectory();
// Initialize storage system
//----------------------------------------------------------------------------
CORE.Storage.basePath = GetWorkingDirectory(); // Define base path for storage
//----------------------------------------------------------------------------

TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (SDL): Initialized successfully");

return 0;
}

// Close platform
void ClosePlatform(void)
{
SDL_FreeCursor(platform.cursor); // Free cursor
Expand All @@ -1219,7 +1235,7 @@ void ClosePlatform(void)
SDL_Quit(); // Deinitialize SDL internal global state
}


// Scancode to keycode mapping
static KeyboardKey ConvertScancodeToKey(SDL_Scancode sdlScancode)
{
if (sdlScancode >= 0 && sdlScancode < SCANCODE_MAPPED_NUM)
Expand Down
Loading

0 comments on commit a0f0034

Please sign in to comment.