I got back to SDL2 again and I have try to make Cleaner code by doing this
#include <SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main()
{
// initialize SDL
initSDL();
// Draw Everythings on the screen
Draw_Everythings();
}
void initSDL()
{
// initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow("Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if (g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return 1; // sdl could not initialize
}
}
void Draw_Everythings()
{
// everything succeeded lets draw the window
// set to black // This function expects Red, Green, Blue and
// Alpha as color values
SDL_SetRenderDrawColor(g_pRenderer, 255, 0, 0, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
// show the window
SDL_RenderPresent(g_pRenderer);
// set a delay before quitting
SDL_Delay(5000);
// clean up SDL
SDL_Quit();
return 0;
}
I got 2 errors and 5 warnings for some reason as they say
function declared with formal parameter list - Warnings
initSDLâ undefined; assuming extern returning int - Warnings
Draw_Everythingsâ undefined; assuming extern returning int - Warnings
initSDL : âvoidâ function returning a value - Warnings
Draw_Everythings : âvoidâ function returning a value - Warnings
âinitSDLâ: redefinition; different basic types - Error
Draw_Everythings : redefinition; different basic types - Errors
You say "Make sure the functions have been declared before you call them.
If you want a function to return a value then the return type should not be void ."
Oh right and I am trying my best try get cleaner code and I have fixed the errors stuffsâŚ
#include <SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main()
{
// initialize SDL
initSDL();
// Draw Everythings on the screen
Draw_Everythings();
}
int initSDL(void)
{
// initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow("Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if (g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return 1; // sdl could not initialize
}
}
int Draw_Everythings(void)
{
// everything succeeded lets draw the window
// set to black // This function expects Red, Green, Blue and
// Alpha as color values
SDL_SetRenderDrawColor(g_pRenderer, 255, 0, 0, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
// show the window
SDL_RenderPresent(g_pRenderer);
// set a delay before quitting
SDL_Delay(5000);
// clean up SDL
SDL_Quit();
return 0;
}
Is that best way to code it as then I added more function like Menu() , Game() and Game_Over() inside main() ?
Make sure initSDLalways returns something. It doesnât really matter now since you donât actually use the return value but I guess you probably want to do that later. Otherwise, whatâs the point of returning anything?
Itâs difficult to have much opinion on the program when itâs this simple. When things get more complicated you might need to structure it differently.
I think functions should do what the name says. Draw_Everythings does more than just draw. It also waits and shuts down SDL which probably belongs somewhere else.
Function names are usually verbs because they do things. You mention the function names âMenuâ, âGameâ and âGame_Overâ but these are not verbs so I donât know what they would do. They sound more like some kind of âstateâ that might be better represented using data.