Cannot run this simple window( driving me crazy!)

ok i run the code on visual studio 2017 and i get this error:
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol SDL_main referenced in function main_utf8

project’s include and lib folder for linker are correct and SDL2.lib and SDL2main.lib are included

here’s the source code:

main.cpp

#include "Game.hpp"

Game * game = nullptr;

int main(int argc, const char * argv[]) {

	game = new Game();

	game->init("BirchEngine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

	while (game->running()) {

		game->handleEvents();
		game->update();
		game->render();
	}

	game->clean();

	return 0;
}

Game.cpp

#include "Game.hpp"

Game::Game()
{}
Game::~Game()
{}

void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{
	int flags = 0;
	if (fullscreen)
	{
		flags = SDL_WINDOW_FULLSCREEN;
	}

	if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
	{
		std::cout << "Subsystems Initialized!..." << std::endl;

		window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
		if (window)
		{
			std::cout << "Window Created!" << std::endl;
		}

		renderer = SDL_CreateRenderer(window, -1, 0);
		if (renderer)
		{
			SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
			std::cout << "Renderer created!" << std::endl;
		}

		isRunning = true;
	} else {
		isRunning = false;
	}
}

void Game::handleEvents()
{
	SDL_Event event;
	SDL_PollEvent(&event);
	switch (event.type) {
	case SDL_QUIT:
		isRunning = false;
		break;
	}
}

void Game::update()
{}

void Game::render()
{
	SDL_RenderClear(renderer);
	//this is where we would add stuff to render
	SDL_RenderPresent(renderer);
}

void Game::clean()
{
	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(renderer);
	SDL_Quit();
	std::cout << "Game Cleaned" << std::endl;
}

Game.hpp

#ifndef __GAME__
#define __GAME__

#include "SDL.h"
#include <iostream>

class Game {

public:
	Game();
	~Game();

	void init(const char * title, int xpos, int ypos, int width, int height, bool fullscreen);
	
	void handleEvents();
	void update();
	void render();
	void clean();

	bool running() { return isRunning; };

private:
	bool isRunning;
	SDL_Window * window;
	SDL_Renderer * renderer;
};

#endif // !__GAME__

changed
int main(int argc, const char * argv[])
to
int main(int argc, char * argv[])

it fixed the problem. :smiley:

1 Like

is it normal for this code to set cpu at 100% in task manager!!??
the debugger in visual studio also show 45% cpu usage which is A LOT for a white screen

Try SDL_RENDERER_PRESENTVSYNC flag in creation of renderer or put a delay in your main loop as long as you don’t have timing code. Atm your code runs as fast as possible.
Check https://gafferongames.com/post/fix_your_timestep/
or/and have a look on the readme in this repo https://github.com/Acry/simple-state-pattern
-Cass

But SDL Development is about SDL Development =)