Cleaner Code?

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

What I am doing wrong?

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 .

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 initSDL always 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.

1 Like