Chaos Project

Game Development => Sea of Code => Topic started by: G_G on December 24, 2011, 12:35:18 am

Title: Visual C++ 2010: Link Error
Post by: G_G on December 24, 2011, 12:35:18 am
I've been experimenting with C++ and SDL. Things were going good until I tried to compile it under the "Release" configuration. I simple switched from Debug to Release and got this error.
Error	1	error LNK2001: unresolved external symbol _WinMain@16	c:\Users\ronnie\documents\visual studio 2010\Projects\SDL\SDL\MSVCRT.lib(crtexew.obj)	SDL

I created an empty C++ project and after doing some googling, I found out that if I want it to be a windows application, it needs to have the function winmain. I've also found posts where people said to put "wWinMainCRTStartup" as my entry point, which didn't work. I managed to get it to run if I set the sub system to Console, but I don't want an annoying empty console behind my game. What I don't understand is how the project is able to run under "Debug" without the WinMain method. What I'd like to know is how to fix the above error and keep the project a non-console application.

Here's my main.cpp file.
Spoiler: ShowHide
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <string>
#include "SpriteSheet.h"
#include <SDL_mixer.h>
#undef main

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface* screen = NULL;
SDL_Surface* image = NULL;
SDL_Surface* text = NULL;
SpriteSheet* sheet = NULL;

SDL_Event event;

TTF_Font* font;
SDL_Color text_color = {255, 255, 255};

bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL)
return false;
if (TTF_Init() == -1)
return false;
SDL_WM_SetCaption("Shooter", NULL);
return true;
}

Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
    Uint32 *pixels = (Uint32 *)surface->pixels;
    return pixels[ ( y * surface->w ) + x ];
}

void put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
    Uint32 *pixels = (Uint32 *)surface->pixels;
    pixels[ ( y * surface->w ) + x ] = pixel;
}

SDL_Surface *loadImage(std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if (loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
if (optimizedImage != NULL)
{
Uint32 colorKey = get_pixel32(optimizedImage, 0, 0);
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorKey);
}
return optimizedImage;
}

bool load_files()
{
image = loadImage("001-Fighter01.png");
sheet = new SpriteSheet(image, 4, 4, false);
font = TTF_OpenFont("BurstMyBubble.ttf", 28);
if (font == NULL)
return false;
if (image == NULL)
return false;
return true;
}

bool clean_up()
{
SDL_FreeSurface(image);
SDL_FreeSurface(text);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return true;
}

void blitSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* srcrect, SDL_Rect* target)
{
SDL_BlitSurface(source, srcrect, destination, target);
}

int main(int argc, char* args[])
{
bool quit = false;
    if (!init())
return 1;
load_files();
text = TTF_RenderText_Solid(font, "lol. Animation.", text_color);
int frame = 0;
int tick = 0;
SDL_Rect message;
message.x = 4;
message.y = 4;
SDL_Rect sprite;
sprite.x = 32;
sprite.y = 32;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
quit = true;
else if (event.type = SDL_KEYDOWN)
{
switch( event.key.keysym.sym )
                {
                    case SDLK_UP:
sprite.y -= 4;
break;
                    case SDLK_DOWN:
sprite.y += 4;
break;
                    case SDLK_LEFT:
sprite.x -= 4;
break;
                    case SDLK_RIGHT:
sprite.x += 4;
break;
                }
}
}
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));
blitSurface(4, 4, text, screen, NULL, &message);
tick++;
blitSurface(32, 32, image, screen, sheet->frame(frame), &sprite);
if (tick > 100)
{
tick = 0;
frame++;
}
if (frame > 15)
frame = 0;
if (SDL_Flip(screen) == -1)
return 1;
}
clean_up();
    return 0;   
}

Title: Re: Visual C++ 2010: Link Error
Post by: Blizzard on December 25, 2011, 02:49:54 pm
"int main" is used for Console programs. Programs using the Windows subsystem require a "WinMain" function. Google it to find out how to use it. Also specify in the projects settings (Linker->System) that you have set the "SubSystem" to the WINDOWS subsystem and not CONSOLE.