NO

Author Topic: SDL  (Read 2367 times)

peekstheclown

  • Guest
SDL
« on: August 31, 2013, 04:25:57 AM »
I am setting up SDL 1.2 and I get this error. Here is my code:
Code: [Select]
#include <stdio.h>
#include <SDL.h>

#define WIDTH 1366
#define HEIGHT 768
#define BPP 4
#define DEPTH 32

void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
    Uint32 *pixmem32;
    Uint32 colour; 
 
    colour = SDL_MapRGB( screen->format, r, g, b );
 
    pixmem32 = (Uint32*) screen->pixels  + y + x;
    *pixmem32 = colour;
}


void DrawScreen(SDL_Surface* screen, int h)
{
    int x, y, ytimesw;
 
    if(SDL_MUSTLOCK(screen))
    {
        if(SDL_LockSurface(screen) < 0) return;
    }

    for(y = 0; y < screen->h; y++ )
    {
        ytimesw = y*screen->pitch/BPP;
        for( x = 0; x < screen->w; x++ )
        {
            setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
        }
    }

    if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
 
    SDL_Flip(screen);
}


int main(int argc, char* argv[])
{
    SDL_Surface *screen;
    SDL_Event event;
 
    int keypress = 0;
    int h=0;
 
    if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   
    if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN|SDL_HWSURFACE)))
    {
        SDL_Quit();
        return 1;
    }
 
    while(!keypress)
    {
         DrawScreen(screen,h++);
         while(SDL_PollEvent(&event))
         {     
              switch (event.type)
              {
                  case SDL_QUIT:
              keypress = 1;
              break;
                  case SDL_KEYDOWN:
                       keypress = 1;
                       break;
              }
         }
    }

    SDL_Quit();
 
    return 0;
}
I got this from here: http://www.friedspace.com/cprogramming/sdlbasic.php

Here is my error:

Code: [Select]
Building engine.exe.
POLINK: error: Unresolved external symbol '_SDL_main@8'.
POLINK: fatal error: 1 unresolved external(s).
*** Error code: 1 ***
Done.

Attached is my full project folder.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: SDL
« Reply #1 on: August 31, 2013, 10:33:36 AM »
SDL uses __cdecl
Use it with sdlmain.lib project
Quote
Set Compiler Options
 Go to Project->Project Options->Compiler and tick the box that says "Enable Microsoft Extensions" and set "Calling conv" to "__cdecl" in the drop down box.
SDL_win32_main.c can found from here

EDIT:
If someone doesn't care about portability and don't want to use SDL_win32_main
Code: [Select]
#include <stdio.h>
#include "SDL/SDL.h"

#pragma comment(lib, "sdl.lib")
//#pragma comment(lib, "sdl_main.lib")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")

#define WIDTH 640
#define HEIGHT 480
#define BPP 4
#define DEPTH 32

void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 *pixmem32;
Uint32 colour;

colour = SDL_MapRGB(screen->format, r, g, b);

pixmem32 = (Uint32 *)screen->pixels + y + x;
*pixmem32 = colour;
}


void DrawScreen(SDL_Surface *screen, int h)
{
int x, y, ytimesw;

if (SDL_MUSTLOCK(screen)) {
if (SDL_LockSurface(screen) < 0)
return;
}

for (y = 0; y < screen->h; y++) {
ytimesw = y * screen->pitch / BPP;
for (x = 0; x < screen->w; x++) {
setpixel(screen, x, ytimesw, (x * x) / 256 + 3 * y + h, (y * y) / 256 + x + h, h);
}
}

if (SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);

SDL_Flip(screen);
}

#ifdef _WIN32
int __stdcall WinMain(int hInstance, int hPrevInstance, char* lpszCmdLine, int nCmdShow)
//int __cdecl WinMainCRTStartup(void)
#else
int __cdecl main(int argc, char *argv[])
#endif
{
SDL_Surface *screen;
SDL_Event event;

int keypress = 0;
int h = 0;

if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;

//if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN|SDL_HWSURFACE)))
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_SWSURFACE | SDL_RESIZABLE))) {
SDL_Quit();
return 1;
}

while (!keypress) {
DrawScreen(screen, h++);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
keypress = 1;
break;
case SDL_KEYDOWN:
keypress = 1;
break;
case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode( event.resize.w, event.resize.h, DEPTH, SDL_SWSURFACE | SDL_RESIZABLE);
break;
}
}
}

SDL_Quit();

return 0;
}
« Last Edit: August 31, 2013, 07:09:58 PM by timovjl »
May the source be with you