Jump to content

  • Log In with Google      Sign In   
  • Create Account


Nazzrim

Member Since 09 May 2012
Offline Last Active Jan 12 2014 02:13 PM
-----

Topics I've Started

Proper DLL use

29 May 2012 - 04:46 AM

Hey everyone.
I am learning SDL for some month now, which dramatically increased my C++ skills.

But I have some questions about the use/general function of DLL files:

1. My interpretation of DLL file is a collection of functions and classes (not necessarily C/C++) provided for other programs. Is this right?

2. On different sources I read about "static linking" & "dynamic linking" but I am not completely sure about it's meanings:

If I use #include <SDL/SDL.h> the header file for SDL.dll is directly included into my project when compiled. I hope till this point I am not wrong...
This way the DLL is linked static because the specific header for this DLL version was included, isn't is?
So you can't exchange the DLL version because of eventually lacks of compatibility?

And if I want to link a DLL dynamic I include <windows.h>. I have a bunch of typedefs for the used functions like:
typedef int (*SDL_Init_i) (Uint32);

And then I have to get the address of the called function at runtime with some code like this:
SDL_Init_i iSDL_init;
HINSTANCE hSDL = LoadLibrary("SDL.dll");
if (hSDL)
{
iSDL_init = (SDL_Init_i) GetProcAddress(hSDL, "SDL_init");

if ( !iSDL_init)
{
  return 1;
}
}
else
{
return 1;
}

After that I hopefully can use the function probably this way for example:
if (iSDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
{
log->log("SDL init error...");
return false;
}

At the end of my programm I have to free my program with FreeLibrary(hSDL);.

This way I have the possibility of changing the SDL.dll for example with a newer version, haven't I?

So now the actual questions for 2. ;):
- Is my knowledge about DLL linking correct?
- If it is: Isn't there a cross platform solution for dynamic linking or: how would this be done in a Linux environment?
- I have chosen the example above because it shows an other problem: If I link a DLL dynamic, how can I use special flags or classes out of this DLL (like SDL_INIT_VIDEO or SDL_Rect as class)?

3. If I am compiling I always use the following flags: -lSDL -lSDLmain. I know they are used for the Linker and they tell what to use but why are there two for one DLL? If I also use SDL_image.DLL I have to add -lSDL_image. So exactly: What is the -lSDLmain flag for?

3.a. Is there a difference in linking static/dynamic and the use of flags? Because I "hardcode" the DLL filepath when linking dynamic, so there is no need of extra compiler flags?

4. Why are there Runtime Libraries & Development Libraries? Just a missing documentation or what is the difference?

5. Inside the lib folder of SDL there are .a files. What are they used for? I read some tutorials about DLL's and the .a files are a product of creating DLL's (surprise...). Are they part of dynamic/static links or are they needed always?

EDIT:
6. If I link dynamic: Is it better to include all needed DLL-functions/classes/etc. at once at the beginning, or should I load the DLL every time a function is needed?
I am not sure because I don't know how fast DLL's are. Maybe they are a bottleneck, I don't know;). Or it would consume too much memory to save all function-addresses the whole Runtime?

I hopefully didn't scare you with my bad English skills and/or my newbie problems;).

I am thankful for every help.

Nazrim

[C++] suddenly shutdown on some computers

09 May 2012 - 03:35 PM

Hey everyone.

At first: Please excuse my poor English skills :-(.

But let me tell you about my problem:

I wanted to lean how to use SDL. So, at first I read some articles and tutorials. Then I started to code a "game", less a game, more object of my personal study.

I want to create a framework, which grows with my knowledge, where I can implement my ideas and learn out of it.

I have chosen C++ because I already have some Skills in it and want to improve them.

This is my working state:

- I am using Windows 7 64bit Professional

- game-state-machine with actual 2 states (menu & "game"-mode)

- button, timer, tileMap, animation, player, bitmapFont & log classes

- menu state shows a background, with a barely animated cloud and a button, which switches to gameState

- game-mode has a tileMap based background with a level read in by a file, A test of the bitmapFont, and a moveable "hero" with a camara following him (2D if there are any uncertainties) and an exit button.

All this works fine on the computer I coded it on.

Now the actual problem:

I placed the compiled .exe and all necessary .dll's and images in my DropBox because I wanted to test it on my notebook and I direct linked the folder with a fellow student.

On the source computer it isn't possible to start the game, direct ".exe doesn't work anymore..." (- hope I translated this one right -).

On my Notebook (also Windows 7 64bit Professional / no seperate SDL installation) the game starts correct and enters the main Menu, but if click on the new Game button the game closes without any error message.

So I set up my log and logged every step happening (See code & log below).

This way I pointed out that the program exits without a reason (visible for me) after the end of the constructor of the exit button in game state.

(This is the most important problem)

On the computer of my friend (he actually tested with ArchLinux) it works fine in some cases or it isn't startable. (Also interesting, but it's more important for me to get a working version on my notebook, but I am interested in articles/tutorials about using SDL cross platform, because reading windows and linux tutorials and then point out the differences isn't fun, I think.)

Edit: Talked to my friend again, he used windows, too, but I am still interested in cross platform sources.


I am thankful for every help and advice you can provide me. :-)


If new Game button is clicked nxtState is set to STATE_HOME_FARM, changeState is called every gameloop before rendering.

void changeState()
{
if (nxtState != STATE_NO_CHANGE)
{
  log->log("start change");
  if (nxtState != STATE_EXIT)
  {
   log->log("delete act");
   delete workingState;
   log->log("done");
  }
  switch (nxtState)
  {
   case STATE_HOME_FARM:
	log->log("detected home farm");
	workingState = new HomeFarm();
	log->log("createt state");
	break;
  }
  log->log("set vars");
  actState = nxtState;
  nxtState = STATE_NO_CHANGE;
  log->log("done");
}
}


This is the constructor of my HomeFarm class.

actWid = LEVEL_WIDTH;
actHig = LEVEL_HEIGHT;
log->log("create hero");
hero = new Farmer;
log->log("create exit button");
exitButton = new ExitButton(SCREEN_WIDTH/2, SCREEN_HEIGHT-200 , 200, 100, "Buttons/test_button_exit.png");
log->log("create font");


This is the constructor of my ExitButton class

log->log("set vars");
CLIP_MOUSEOVER = 0;
CLIP_MOUSEOUT = 1;
CLIP_MOUSEDOWN = 2;
CLIP_MOUSEUP = 3;
button.x = x;
button.y = y;
button.w = width;
button.h = height;
log->log("load img");
img = loadImg(filename);
log->log("set clip");
setClips();
log->log("set initial clip");
state = &clip[CLIP_MOUSEOUT];
log->log("done");


And finally this is my log.txt created if I run the game on my notebook and try to start a Game.

start change
delete act
done
detected home farm
create hero
create exit button
set vars
load img
set clip
set initial clip
done

Nazzrim


PARTNERS