NO

Author Topic: Setting up SDL in Pelles C  (Read 25401 times)

LW

  • Guest
Setting up SDL in Pelles C
« on: June 04, 2010, 11:07:58 AM »
Here's an useful tutorial about how to set up Simple Directmedia Layer library in Pelles C:

http://www.friedspace.com/cprogramming/sdlsetup.php

MirekCZ

  • Guest
Re: Setting up SDL in Pelles C
« Reply #1 on: May 31, 2011, 12:01:33 AM »
Here's an useful tutorial about how to set up Simple Directmedia Layer library in Pelles C:

http://www.friedspace.com/cprogramming/sdlsetup.php

Today: 1.6.2011. I test SDL 1.2.14 on Pelles6.50 rel..candidate. It is still functional! This is is perfect cookbook how to setup. Step by step (or "microstep"). 1/Small appendix: be aware to switch ON Project/Option/Compiler [v] Enable Micr. extensions (It's not default).
2/Comments:
BTWay: does anybody know, how to use static linking with DLL? I need to distrib. my small funny game(s) like only one exe file. Manhy thanks.
« Last Edit: May 31, 2011, 08:22:23 AM by MirekCZ »

FragNox

  • Guest
Re: Setting up SDL in Pelles C
« Reply #2 on: August 16, 2011, 11:04:54 AM »
Will this procedure continue to work with the revised new version 1.3?

CommonTater

  • Guest
Re: Setting up SDL in Pelles C
« Reply #3 on: August 16, 2011, 05:03:32 PM »
I don't know if I'd want to be copying 3rd party files into the include and lib folders for Pelles C.... It would be a lot smarter to keep SDL in it's own folders and modify the Project->Project Options->Folders as needed for projects that include SDL. 

FragNox

  • Guest
Re: Setting up SDL in Pelles C
« Reply #4 on: August 17, 2011, 04:23:19 AM »
I don't know if I'd want to be copying 3rd party files into the include and lib folders for Pelles C.... It would be a lot smarter to keep SDL in it's own folders and modify the Project->Project Options->Folders as needed for projects that include SDL.

That exactly what I do in this situation. Keep external things separate, the same goes for DirectX.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Setting up SDL in Pelles C
« Reply #5 on: August 17, 2011, 10:39:47 AM »
You never know what you break, if you include too many extensions by default.
---
Stefan

Proud member of the UltraDefrag Development Team

FragNox

  • Guest
Re: Setting up SDL in Pelles C
« Reply #6 on: August 18, 2011, 11:00:22 AM »
I threw together a little hack for this using __stdcall calling convention it's essentially the same as the original SDL_win32_main.c with the calls to atexit() removed and the SDL exit routines placed before exit is called to close the program.

Code: [Select]
/*
    SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98

    The WinMain function -- calls your program's main() function
*/
/********************************************************************************/
/*
Changed a few things to make this compile with Pelles C with the __stdcall
calling cnvention. Basically removed references to and placed all clean up
functions before exit is called.

~Saxon Bell, 2011
*/

#include <stdio.h>
#include <stdlib.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#ifdef _WIN32_WCE
# define DIR_SEPERATOR TEXT("\\")
# undef _getcwd
# define _getcwd(str,len) wcscpy(str,TEXT(""))
# define setbuf(f,b)
# define setvbuf(w,x,y,z)
# define fopen _wfopen
# define freopen _wfreopen
# define remove(x) DeleteFile(x)
#else
# define DIR_SEPERATOR TEXT("/")
# include <direct.h>
#endif

/* Include the SDL main definition header */
#include "SDL.h"
#include "SDL_main.h"

#ifdef main
# ifndef _WIN32_WCE_EMULATION
#  undef main
# endif /* _WIN32_WCE_EMULATION */
#endif /* main */

/* The standard output files */
#define STDOUT_FILE TEXT("stdout.txt")
#define STDERR_FILE TEXT("stderr.txt")

/* Set a variable to tell if the stdio redirect has been enabled. */
static int stdioRedirectEnabled = 0;

#ifdef _WIN32_WCE
  static wchar_t stdoutPath[MAX_PATH];
  static wchar_t stderrPath[MAX_PATH];
#else
  static char stdoutPath[MAX_PATH];
  static char stderrPath[MAX_PATH];
#endif

#if defined(_WIN32_WCE) && _WIN32_WCE < 300
/* seems to be undefined in Win CE although in online help */
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
#endif /* _WIN32_WCE < 300 */

static void UnEscapeQuotes( char *arg )
{
char *last = NULL;

while( *arg ) {
if( *arg == '"' && *last == '\\' ) {
char *c_curr = arg;
char *c_last = last;

while( *c_curr ) {
*c_last = *c_curr;
c_last = c_curr;
c_curr++;
}
*c_last = '\0';
}
last = arg;
arg++;
}
}

/* Parse a command line buffer into arguments */
static int ParseCommandLine(char *cmdline, char **argv)
{
char *bufp;
char *lastp = NULL;
int argc, last_argc;

argc = last_argc = 0;
for ( bufp = cmdline; *bufp; ) {
/* Skip leading whitespace */
while ( isspace(*bufp) ) {
++bufp;
}
/* Skip over argument */
if ( *bufp == '"' ) {
++bufp;
if ( *bufp ) {
if ( argv ) {
argv[argc] = bufp;
}
++argc;
}
/* Skip over word */
while ( *bufp && ( *bufp != '"' || *lastp == '\\' ) ) {
lastp = bufp;
++bufp;
}
} else {
if ( *bufp ) {
if ( argv ) {
argv[argc] = bufp;
}
++argc;
}
/* Skip over word */
while ( *bufp && ! isspace(*bufp) ) {
++bufp;
}
}
if ( *bufp ) {
if ( argv ) {
*bufp = '\0';
}
++bufp;
}

/* Strip out \ from \" sequences */
if( argv && last_argc != argc ) {
UnEscapeQuotes( argv[last_argc] );
}
last_argc = argc;
}
if ( argv ) {
argv[argc] = NULL;
}
return(argc);
}

/* Show an error message */
static void ShowError(const char *title, const char *message)
{
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
#ifdef USE_MESSAGEBOX
MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
#else
fprintf(stderr, "%s: %s\n", title, message);
#endif
}

/* Pop up an out of memory message, returns to Windows */
static BOOL OutOfMemory(void)
{
ShowError("Fatal Error", "Out of memory - aborting");
return FALSE;
}

/* Remove the output files if there was no output written */
static void cleanup_output(void) {
FILE *file;
int empty;

/* Flush the output in case anything is queued */
fclose(stdout);
fclose(stderr);

/* Without redirection we're done */
if (!stdioRedirectEnabled) {
return;
}

/* See if the files have any output in them */
if ( stdoutPath[0] ) {
file = fopen(stdoutPath, TEXT("rb"));
if ( file ) {
empty = (fgetc(file) == EOF) ? 1 : 0;
fclose(file);
if ( empty ) {
remove(stdoutPath);
}
}
}
if ( stderrPath[0] ) {
file = fopen(stderrPath, TEXT("rb"));
if ( file ) {
empty = (fgetc(file) == EOF) ? 1 : 0;
fclose(file);
if ( empty ) {
remove(stderrPath);
}
}
}
}

/* Redirect the output (stdout and stderr) to a file */
static void redirect_output(void)
{
DWORD pathlen;
#ifdef _WIN32_WCE
wchar_t path[MAX_PATH];
#else
char path[MAX_PATH];
#endif
FILE *newfp;

pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
while ( pathlen > 0 && path[pathlen] != '\\' ) {
--pathlen;
}
path[pathlen] = '\0';

#ifdef _WIN32_WCE
wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#else
SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#endif
   
/* Redirect standard input and standard output */
newfp = freopen(stdoutPath, TEXT("w"), stdout);

#ifndef _WIN32_WCE
if ( newfp == NULL ) { /* This happens on NT */
#if !defined(stdout)
stdout = fopen(stdoutPath, TEXT("w"));
#else
newfp = fopen(stdoutPath, TEXT("w"));
if ( newfp ) {
*stdout = *newfp;
}
#endif
}
#endif /* _WIN32_WCE */

#ifdef _WIN32_WCE
wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#else
SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
#endif

newfp = freopen(stderrPath, TEXT("w"), stderr);
#ifndef _WIN32_WCE
if ( newfp == NULL ) { /* This happens on NT */
#if !defined(stderr)
stderr = fopen(stderrPath, TEXT("w"));
#else
newfp = fopen(stderrPath, TEXT("w"));
if ( newfp ) {
*stderr = *newfp;
}
#endif
}
#endif /* _WIN32_WCE */

setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
setbuf(stderr, NULL); /* No buffering */
stdioRedirectEnabled = 1;
}

#if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(__POCC__) //make sure it isn't actually POCC
/* The VC++ compiler needs main defined */
#define console_main main
#endif

/* This is where execution begins [console apps] */
int console_main(int argc, char *argv[])
{
size_t n;
char *bufp, *appname;
int status;

/* Get the class name from argv[0] */
appname = argv[0];
if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
appname = bufp+1;
} else
if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
appname = bufp+1;
}

if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
n = SDL_strlen(appname);
else
n = (bufp-appname);

bufp = SDL_stack_alloc(char, n+1);
if ( bufp == NULL ) {
return OutOfMemory();
}
SDL_strlcpy(bufp, appname, n+1);
appname = bufp;

/* Load SDL dynamic link library */
if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
ShowError("WinMain() error", SDL_GetError());
return(FALSE);
}

/* Sam:
   We still need to pass in the application handle so that
   DirectInput will initialize properly when SDL_RegisterApp()
   is called later in the video initialization.
*/
SDL_SetModuleHandle(GetModuleHandle(NULL));

/* Run the application main() code */
status = SDL_main(argc, argv);

cleanup_output();
SDL_Quit();
/* Exit cleanly, calling atexit() functions */
exit(status);

/* Hush little compiler, don't you cry... */
return 0;
}

/* This is where execution begins [windowed apps] */
#ifdef _WIN32_WCE
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
#else
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
#endif
{
HINSTANCE handle;
char **argv;
int argc;
char *cmdline;
char *env_str;
#ifdef _WIN32_WCE
wchar_t *bufp;
int nLen;
#else
char *bufp;
size_t nLen;
#endif

/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   keep them open.  This is a hack.. hopefully it will be fixed
   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
*/
handle = LoadLibrary(TEXT("DDRAW.DLL"));
if ( handle != NULL ) {
FreeLibrary(handle);
}

/* Check for stdio redirect settings and do the redirection */
env_str = SDL_getenv("SDL_STDIO_REDIRECT");
if ((env_str)) {
if (SDL_atoi(env_str)) {
redirect_output();
}
}
#ifndef NO_STDIO_REDIRECT
else {
redirect_output();
}
#endif

#ifdef _WIN32_WCE
nLen = wcslen(szCmdLine)+128+1;
bufp = SDL_stack_alloc(wchar_t, nLen*2);
wcscpy (bufp, TEXT("\""));
GetModuleFileName(NULL, bufp+1, 128-3);
wcscpy (bufp+wcslen(bufp), TEXT("\" "));
wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
nLen = wcslen(bufp)+1;
cmdline = SDL_stack_alloc(char, nLen);
if ( cmdline == NULL ) {
return OutOfMemory();
}
WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
#else
/* Grab the command line */
bufp = GetCommandLine();
nLen = SDL_strlen(bufp)+1;
cmdline = SDL_stack_alloc(char, nLen);
if ( cmdline == NULL ) {
return OutOfMemory();
}
SDL_strlcpy(cmdline, bufp, nLen);
#endif

/* Parse it into argv and argc */
argc = ParseCommandLine(cmdline, NULL);
argv = SDL_stack_alloc(char*, argc+1);
if ( argv == NULL ) {
return OutOfMemory();
}
ParseCommandLine(cmdline, argv);

/* Run the main program (after a little SDL initialization) */
console_main(argc, argv);

/* Hush little compiler, don't you cry... */
return 0;
}

VictorGerth

  • Guest
Re: Setting up SDL in Pelles C
« Reply #7 on: July 02, 2020, 03:12:42 AM »
The link is dead (I'm in the USA)

Offline quadcricket

  • Member
  • *
  • Posts: 3
Re: Setting up SDL in Pelles C
« Reply #8 on: June 01, 2021, 12:46:42 AM »
The link is dead (I'm in the USA)

Content from Waybackmachine:

Code: [Select]
In order to use SDL in Pelles C, you first need to set up the SDL library so that it can be used. This is a simple process. Just follow the steps below. If you have any problems, please log in to our forum and drop us a note. We'll see what we can do to help you.

Special thanks to Ngan Lo for supplying instructions on how to set up SDL in an efficient way and frankie on the Pelles C forum for helping me get SDL working with Pelles C on my own machine after it mysteriously stopped working!!

Download SDL
Go to the SDL webpage, click on the latest version of SDL (version 1.2 at the time of writing) in the download section of the menu on the left hand side of the page. In the source code section, download the source code zip file and in the development libraries, download the Win32 (Visual C++ 6.0) zip file.
Create a Pelles C Project
Click File->New->Project. Select "Win32 Static Library (.lib)" and call it sdlmain. Take note of the location of the project directory that will be created.
Extract the SDL source code
Inside the source code zip file you will find a src\main\win32 directory. Copy the file SDL_win32_main.c to the new Pelles C project directory you created.
Extract the Include Files
Copy all the files from the include directory of the source code zip file into Pelles C's Include directory.
Add sdl_win32_main.c to the Project
Click Project->Add files to project, and select the file sdl_win32_main.c.
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.
Build the Library
Click Projects->Build sdlmain.lib. It will sit there for some time building (though it may look like nothing is happening). Eventually in the pane at the bottom it will say "Done".
Copy the .lib File
In your sdlmain project directory, you will now find the library you have just built (sdlmain.lib). Copy this to Pelles C's Lib directory.
Copy the other Libraries
Unzip the developers libraries zip file and copy the file sdl.lib to Pelles C's Lib directory. Copy the file sdl.dll to the Pelles C Bin directory. These files are in the lib folder of the zip file. Again you are probably better off extracting the whole zip file and then just copying the files you need.
Remove old copies of SDL.DLL from your machine.
Do a search of your Windows directory on you machine, and all its subdirectories, especially Windows\system and ensure there are no old copies of SDL.DLL there as these will interfere with the copy that you have placed in your PellesC\Bin directory. If you need SDL for other things, place the dll file in the Windows\system directory instead of PellesC\Bin.