PDA

View Full Version : 2D using OpenGL



Smotsholle
04-04-2006, 09:41 AM
I've searched many sites for this one and I haven't quite been able to find that what I'm looking for.

As many of you know, I used SDL to make my previous gaming project (Caimans quest), and this time I want to make a new game now for a school.

It's supposed to be an educational game for children around the age of 11.

After running some tests and demos on the pcs here I found out OpenGL is the best way to go.

Now I'm everything but experienced in OpenGL, but I found out via Tilestudio's website it can be done, and the demo looks great.

After doing some research I found out they used the Clean library for sprites, it looks nice and everything, but I want to build my own mapmaker and build my own engine. Clean is a bit too complete and is difficult to change too much, not my type of engine.

What I really want is a small demo or tutorial to get me started on placing some 2D sprites on a background.

What I want to know at first is:
- How to get started using this? (Tutorial or sourcecode would suffice)
- How to animate a sprite on a polygon in 2D?

Being able to place a 2d object and move it around in 2D would suffice for me at first.
Just need a little boost start and I can start programming :D

User137
04-04-2006, 10:36 AM
This is propably the best site there is, many tutorials with delphi source
http://nehe.gamedev.net/

Then i would point out this link for the latest OpenGL 1.9 header (Library section under Files) and lots of demos too. Too bad it's mostly german site :P
http://www.delphigl.com/

Those sites i used when creating GLEngine (under development), in some level as easy as DelphiX but faster and better graphics...

savage
04-04-2006, 11:53 AM
In JEDI-SDL I ported quite a few of the Nehe OpenGL tutorials, most can be found on the Nehe site or in the JEDI-SDL sourceforge CVS.

cairnswm
04-04-2006, 12:33 PM
All my latest games use SDL and OpenGL. I have written a set of wrapper libraries to make it easy to load images and draw them to the screen. So although its using OpenGL you dont actually need to know OpenGL to use them.

The souce code available for most of my games includes a copy of the wrappers (I call it S2DL - Simple 2D library) - I can also upload the latest version of the wrappers later if you like.

Smotsholle
04-04-2006, 01:33 PM
I've taken the liberty of trying the NeHe site and so far it is going great. I've managed to place some stuff on the screen...

I'm working on a blockbuilder now which places blocks on the screen depending on the integer value of the block (so far, 0=none, 1=block)

Now for texturing...

Thanks for the help, it really works!

Smotsholle
05-04-2006, 09:13 AM
About the wrappers....

Thanks for the offer, but this is a school internship project and I have to show them I can make it all from scratch from the basic delphi components...

Too, bad, but that's the way it is...

Found another problem btw...

I've added a couple of rectangular tiles, but I can't really seem to get a single transparent color. Is there a simple way without using gifs to get this?

So like in SDL I want to set a transparent color (Pink or FF00FF).

I have found a way to animate tiles, but I don't know if it's the most efficient way to go...
I'm filling an array with textures (bitmaps) and each time I redraw the polygon (rectangle) I load a different texture each time. Don't know if it is efficient or not, but if anyone has a better suggestion I'm open to it.

savage
05-04-2006, 10:47 AM
If you are using OpenGL, you need to use a texture format that supports transparency layers, something like png and look up blending ( nehe lesson 8 - http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=8 ). If you are just just looking for a color key type effect then you need to look up masking ( nehe lesson 20 - http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=20 )

I hope this helps.

Traveler
05-04-2006, 10:57 AM
Have a look at these tutorials (http://www.sulaco.co.za/tut.htm). And be sure to check out the download page as well!

Smotsholle
05-04-2006, 01:51 PM
Savage,

I hoped to avoid both those methods to add transparency.
This game will feature a level editor and I want to make it easy on people to change the images.

The biggest problem is that I have to assume the people who want to create their own graphics and such don't have any knowledge of anything which has something to do with drawing pictures.

I hoped to just get them to use paint and copy-paste some stuff.

Now it seems there are only the masking or image transparency layers (PNG or GIF).

Masking needs to work with a seperate file (black/white) or a loop on all pixels (could work, but doesn't sound efficient)

A transparency layer could work, I think.. Just need to add a written tutorial on how to make gifs with transparency.

(You guys must be thinking, what in the world did this guy get into?)

I'll look futher into the masking bit for now... I hope I missed something that will make it all a bit easier on me ;)


BTW traveller. Thanks for another great set of demos... They really helped out so far.... The example for adding text is more clear to me there than in NeHe's example :D

savage
05-04-2006, 02:57 PM
Have a look at the SDL code here -
http://osdl.sourceforge.net/OSDL/OSDL-0.3/src/doc/web/main/documentation/rendering/SDL-openGL-examples.html
It shows another way of achieving colour keyed transparency.

Don't forget that since you are using OpenGL you will need to tell users of your software to create Power of 2 textures if they want the least amount of hassle amongst graphics.

jdarling
05-04-2006, 03:17 PM
Have a look at the SDL code here -
http://osdl.sourceforge.net/OSDL/OSDL-0.3/src/doc/web/main/documentation/rendering/SDL-openGL-examples.html
It shows another way of achieving colour keyed transparency.

Don't forget that since you are using OpenGL you will need to tell users of your software to create Power of 2 textures if they want the least amount of hassle amongst graphics.

Or convert all of the graphics to power of 2 textures on the fly if you don't want to place the limit on your users. Many times you can combine textures and then perform fills as necessary behind the scenes.

Smotsholle
06-04-2006, 06:40 AM
The power of 2 textures shouldn't be a problem. I just told them allready I have a folder of example images... Under no circumstances they are allowed to change the size unless they ask me about it first...

About the tutorial... It uses SDL again, and somehow most computers here have some working fast with SDL.

Can anyone tell me how to do some sort of loop over the pixels of an OpenGL texture and generate a mask accordingly?

Example:
- I have a texture.
- Do a for loop on each pixel.
- If the pixel is pink, add a black pixel on the same coordinates on the mask.
- Otherwise, add a white pixel on the same coordinates on the mask.

Or better yet:
- Load a texture.
- Do a loop on each pixel.
- If pink, change the color to the transparent color (like in gifs)
- Otherwise leave it the way it is.

I believe that should do the trick, but now.... how to do that?

Don't get me wrong here, but this time I want to do something without SDL. The previous school project was all SDL and I don't think the people juding my internship would like me using that again...

Traveler
06-04-2006, 08:19 AM
I'm not sure I understand you entirely, but it is really not needed to have a seperate file to get transparency. Tga files for example have an extra alpha layer (provided they're saved correctly (32 bit)) Using Paint Shop Pro or PhotoShop its fairly easy to create an alpha layer.

The whole suggestion of looping through an image, reading each pixel and change it to a certain color is not only a long and difficult process, it is also completely unnecessary.

Have a look at this (http://www.sulaco.co.za/opengl_project_transparent_TGA_files_and_fog.htm) sample. It has everything you need.

Smotsholle
06-04-2006, 09:22 AM
Guess I'm stuck with either a mask or an image with transparency....

Shouldn't be a really big problem... Just takes some getting used to... I know I can make the TGA or GIF images, but unfortunatelly I'm working on computers where they've only heard of paint :s

I'll just make a written tutorial on how to make them, then.
Either that or a seperate program which makes makes masks of each of my images :D

User137
06-04-2006, 10:13 AM
Or you can simply use alpha channels. Where you would normally give opengl an GL_RGB data, you can also give 4 bytes per pixel, GL_RGBA. This allows using of translucent png textures too. There you could give texture loading function a colorkey.

savage
06-04-2006, 11:26 AM
Guess I'm stuck with either a mask or an image with transparency....

Shouldn't be a really big problem... Just takes some getting used to... I know I can make the TGA or GIF images, but unfortunatelly I'm working on computers where they've only heard of paint :s

I'll just make a written tutorial on how to make them, then.
Either that or a seperate program which makes makes masks of each of my images :D

I could be wrong, but I don't think gif supports true transparency like TGA or Png. I think it only does Color key transparency. There is also the licencing issues with gif as well.

tpascal
06-04-2006, 09:48 PM
Load a texture.
- Do a loop on each pixel.
- If pink, change the color to the transparent color (like in gifs)
- Otherwise leave it the way it is.

I believe that should do the trick, but now.... how to do that?


Actually, your are right; that is the way most people do (including my self) when you wants to use transparency (but avoiding the exra work to use a mask black/white texture) defining a pre-accorded color.

You have to load the whole texture data from file into a block memory and then process every pixel, if the RGB pixel color is 255,0,255 (or any other color you want) then put the alpha component equal to 0, if the pixel IS NOT pinky then put alpha = 1.

How to load a bitmap and inspect/edit every pixel?,

The easy way is to use standars BMP file (you say you preffer to use paint.exe program) in 32bit color format, so for every pixel you wil have 4 bytes (RGBA), tell to your artis to use full 255,0,255 color for define transparent areas in the sprite.

Then in delphi use a temporal TBITMAP for load your bmp file, then allocate a block memory using getmem(pixels, bitmap.width*bitmap.height*4) and then extract there all pixels from the bitmap using Bitmap.scanline() function. Every 4 bytes starting from the pixel pointer are the RGBA pixel color, procces the whole block memory.

*Hint: There is a delphi units out there called GraphicsEX which expand the TIMAGE component to load almost every picture file format (jpg, tga, tif, psd, pcx.. etc) using Timage.picture.loadfromfile() then you can use TBITMAP.canvas.draw(0,0,timage.picture) for convert what ever picture you loaded (in whatever bit color format) into your 24bit color bitmap.

Once you have your picture in memory with the pinky color with alpha=0 then build the texture fragment and enable the apropiate rendering parameters using the API you choose, opengl or directx.

I have implemented this technique full working but using directx, for opengl i found this example:

http://www.codesampler.com/oglsrc/oglsrc_4.htm#ogl_color_key

I have checked and seem the opengl way it is much more easier than in directx...


define the texture like this:


glGenTextures( 1, &g_textureID );
glBindTexture( GL_TEXTURE_2D, g_textureID );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R, GL_NEAREST);

// Don't forget to use GL_RGBA for our new image data... we support Alpha transparency now!
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pImage_RGB->sizeX, pImage_RGB->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA );


Then every time you are going to render the rectangles with transperent pixels do this:



glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

Again, i have not tried that technique in opengl, but in directx, let me know if it work in your opengl test.


* This technique also works in 16 bit color mode, but it takes a litle more work to convert full 32 bit color to 16bit color, this is only necesary if your target machine specs are low.


good luck.

tp.

Smotsholle
07-04-2006, 10:01 AM
I've tried to convert that example source to delphi and I cannot get the .data attribute of a PTAUX_RGBImageRec to give me any info... It is a pointer, but to what? That is an eternal mystery :(
Second problem there is that there isn't a RGBA brother of PTAUX_RGBImageRec.

Tried to get a TBitmap to link to PTAUX_RGBImageRec too, but with no succes. Too bad...

But I've got a demo with masking working, so that's good news :D

User137
07-04-2006, 01:07 PM
if the pixel IS NOT pinky then put alpha = 1.
Actually alpha would be 255 for visible areas, 0 for complete invisibility :wink: But yes, that's the technique i used too.

For data passing it could be something like this:

// Define record for each pixel
TPixelData = record
r,g,b,a: byte;
end;

// Way of using dynamic arrays...
// Expected data is actually 2 dimensional but it is
// continuous data in width blocks
TPixelArray = array[0..0] of TPixelData;
PPixelArray = ^TPixelArray;

// Then to allocate:
procedure LoadTexture(...);
var data: PPixelArray;
begin
data:=allocmem(width*height*sizeof(TPixelData));
// Do data filling and texture loading here...
Freemem(data);
end;

tpascal
07-04-2006, 03:39 PM
Actually alpha would be 255 for visible areas, 0 for complete invisibility But yes, that's the technique i used too.


You are right, i was using 1 value coz i am working in 16 bit color mode (1555 bit scheme), for 32 bit color Alpha=0 mean full transparent, alpha=255 mean full opaque.



I've tried to convert that example source to delphi and I cannot get the .data attribute of a PTAUX_RGBImageRec to give me any info...

That is why i hate C language, :roll:, the example was more meaning to show you the opengl commands and parameters that enable the transparent rendering using alpha channel.

For loading and proccesing the bitmap file please check your delphi help about Tbitmap class methods loadfromfile() and scanline property or search for any custom bitmap file loading rutine out there.

tp.

Smotsholle
11-04-2006, 07:45 AM
Still haven't managed to get it working :(

But at the moment that's no problem to me.

I've got the masking thing going and for now that's enough for me...

Next is to find out how to load a part of a bitmap onto a texture or to show part of a texture on a quad.

I now have a mario type guy running around.
I'm using a seperate folder for each character containing 16 bmp images.
8 for each animation frame and 8 for the masks.

I have come across a problem on that, though...
The program seems to have a slight problem rounding off the textured polygons. When scrolling a very thin black line sometimes appears above my mario dude.
Any idea of what's causing this?

A second question... Anyone have any ideas how to put a maximum FPS on the game? I can't do an infinite loop, but there's probably a way to find the time of the CPU's internal clock or something.

Smotsholle
01-06-2006, 03:21 PM
Just to keep you up to date, I've finished quite a bit of the game allready and I'm trying to work with lighting now.

Managed to do quite a lot on that part.
Behold my lovely museum:
http://webserv.nhl.nl/~kampe001/lightmask1.jpg

This is simply placing a square for each block. I am using 5 layers for this:
layer 1 - Way back there is the sky, not visible here, but it's there
layer 2 - Background blocks used for mountains and other things (in this case an excavation)
layer 3 - Very close background blocks usually used for bridges, flowers or indoor walls.
layer 4 - The usefull layer, places obstacles, the main character and some NPCs.
layer 5 - viewblockers. Things you can walk behind like the darkened eagle statue on the right.

I've also planted a spotlight and lo and behold, it bound itself to the camera.
Very nice for a spotlight tracking the main character (like the camera does), but as you can see there are a couple of bowls of fire on the background too.

It would be great to be able to position a light not relative to the camera position, but to position 0.0 , 0.0 , 0.0 so I can easily place a light on top of the bowl as I create it.

I've spent over 1 hour finding out how to do that, but all I found was vague babbling about it being possible, but no where could I find a single snippet of code showing me how.

So I was wondering, Anyone else had this problem and if so, how to fix it?
Something's telling me I'm overlooking something quite obvious, I usually am.

--EDIT--
Another Thing I found out on the internet was the fact that I can place a maximum of 8 lights on the screen GL_Light0 to GL_Light7, is this true? and if so, I there a way to work around that?

jdarling
01-06-2006, 04:42 PM
--EDIT--
Another Thing I found out on the internet was the fact that I can place a maximum of 8 lights on the screen GL_Light0 to GL_Light7, is this true? and if so, I there a way to work around that?
Yes, alpha maps :)

grudzio
01-06-2006, 09:36 PM
I've spent over 1 hour finding out how to do that, but all I found was vague babbling about it being possible, but no where could I find a single snippet of code showing me how.

So I was wondering, Anyone else had this problem and if so, how to fix it?
Something's telling me I'm overlooking something quite obvious, I usually am.

Before rendering anything, call glLoadIdentity, then position your lights and then setup the camera.

Another Thing I found out on the internet was the fact that I can place a maximum of 8 lights on the screen GL_Light0 to GL_Light7, is this true? and if so, I there a way to work around that?
It is true that OpenGL can use up to eight lights. You can have more lights than that but only eight of them can be used at the same time. So in each frame you should decide which lights are active (up to eight) and use only them. The other possibility to avoid max_eight_lights restriction is to make your own lightning by lightmaps (alphamaps?) or shaders.

Smotsholle
02-06-2006, 08:19 AM
I've been fiddling around with the extra lights, but it gave me too much lighting, but not to worry, I replaced the lights with some viewports showing an extra bit of the excavation.

But I'll certainly keep these advices in mind, maybe something to use for later on.

To show you what's next here's the background for the menu.
I know it isn't possible to get a fixed image as a background fill, but I don't mind that. I'll just make a big quad which is just big enough to fill the background (I'm working with a fixed resolution, so that won't be a problem).
http://webserv.nhl.nl/~kampe001/frontend.jpg
The game will be in dutch and later on commercial (planning to convert it to freepascal later on due to student's license at school), so you won't get to see the full version when it's finished.

Traveler
02-06-2006, 11:11 AM
Working title, I presume? :lol:

Smotsholle
06-06-2006, 06:58 AM
Well this title is actually the definite title.
This title actually came forward from a little discussion.

To translate freely I came up with "Salto and the 'something' cards" on which I asked someone to will something in on the word 'something' (huppelepup in dutch) and actually they found huppelepup to be a funny name so that's how we came up with it (The joke is more of a dutch thing, hard to translate, in short the word is as useless as it sounds).

An extra in this game is the ability to play a parody on the triple triad card game (made more suitable for children of course).

see http://en.wikipedia.org/wiki/Triple_Triad for details on the game. In my version I will forget about the extra rules (same, wall, combo, plus).

The cards will represent characters you meet in the game. and of course these cards are called the huppelepup-cards.

Smotsholle
08-06-2006, 06:39 AM
Ps...

Just wondering if you guys know what can cause an opengl based game to work under win98 and not to work under winXP.

Over here XP has just been installed, wonder why te game suddenly doesn't work.
On xp I only see the background color. (no polys, no textures, no lighting effects)

Traveler
08-06-2006, 07:24 AM
Over here XP has just been installed, wonder why te game suddenly doesn't work
Could be a GFX driver issue. I've never experienced any problems with opengl before in the way you mentioned.

What gfxcard do you have installed?

Smotsholle
08-06-2006, 09:56 AM
It's the exact same pc, just with a different windows.

Found out 2 issues so far on which XP and 98 differs....

One is a zeromemory command I got from a tutorial.
- 98 doesn't see a problem if the memory is allready free.
- XP simply gives an access violation error.
The other one is the order in which I run my initialization commands.
- 98 has no real problems with that.
- XP is more precise on that.

Since it's for public schools I need to calculate the factor they are behind a couple of years (many public schools are) and therefore it needs to be compatible with both win98 and winXP.

Fixed the current problem now (init order) but any other info on this would be welcome).

Traveler
08-06-2006, 10:43 AM
So if you get it to work on XP it also works on 98?

Smotsholle
09-06-2006, 06:49 AM
So far that's been the case.

I've been developing mainly on a win98 machine and a bit on an xp machine.
But now I'm purely going XP, so let's fully test this theory when my game is finished ;)

Smotsholle
12-06-2006, 12:57 PM
I came up with another problem.

I know I can do blending with a mask layer and make parts of a polygon transparent.

Now I want to do a "fade to nothingness" effect.
This means I have a picture I show (partially transparent using a mask layer). I want the whole to become more and more transparent overall until it disappears because it's reached a maximum transparency.

Example:


glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_DST_COLOR,GL_ZERO);
glBindTexture(GL_TEXTURE_2D,backmask[z]);
glBegin(GL_QUADS);
glTexCoord2f( 0.001, 0.99); glVertex3f( i*20-60.0001,-j*20+40.0001, -30.0);
glTexCoord2f( 0.001, 0.01); glVertex3f( i*20-60.0001,-j*20+19.9999, -30.0);
glTexCoord2f( 0.999, 0.01); glVertex3f( i*20-39.9999,-j*20+19.9999, -30.0);
glTexCoord2f( 0.999, 0.99); glVertex3f( i*20-39.9999,-j*20+40.0001, -30.0);
glEnd();
glBlendFunc(GL_ONE,GL_ONE);
glBindTexture(GL_TEXTURE_2D,backtex[z]);
glBegin(GL_QUADS);
glTexCoord2f( 0.001, 0.99); glVertex3f( i*20-60.0001,-j*20+40.0001, -30.0);
glTexCoord2f( 0.001, 0.01); glVertex3f( i*20-60.0001,-j*20+19.9999, -30.0);
glTexCoord2f( 0.999, 0.01); glVertex3f( i*20-39.9999,-j*20+19.9999, -30.0);
glTexCoord2f( 0.999, 0.99); glVertex3f( i*20-39.9999,-j*20+40.0001, -30.0);
glEnd();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

I know there's something possible to make the whole more transparent, but how?
Let's say I want to give it 25% visibility, which is 75% transparency.

Traveler
12-06-2006, 02:36 PM
Look into:

glColor4ub(R,G,B,A);

where rgba are byte values.
In your case its probably glColor4ub(255,255,255,[0..255]);

Nitrogen
12-06-2006, 05:57 PM
Traveller: after having your avatar roll its eyes at me thousands of times, I appreciate the change :)

Smotsholle: I think you may be going about this the wrong way..

First, you should use alpha channels, they make your life a lot easier. Creating the art will be a *little* trickier since you have to use TGA or PNG images.

There is a TGA file loader here which loads a TGA and it's Alpha channel:
http://www.sulaco.co.za/opengl_project_BMP_JPG_TGA_texture_loader.htm
With simple instructions.

You can download a TGA picture to test out here:
http://www.sulaco.co.za/nitrogen/junk/map.tga

Ok, once you have a texture with an alpha channel, this is how you draw it with a 'fade-to-nothing' effect:



glBindTexture(GL_TEXTURE_2D, MyTexture);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //this does the alpha blending.

glcolor4f(1,1,1, FadeAmount); //This does the fading, set FadeAmount to 0 for total transparency, and 1 for full opacity.

glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex2f(X-Size, Y-Size);
glTexCoord2f(1,1); glVertex2f(X+Size, Y-Size);
glTexCoord2f(1,0); glVertex2f(X+Size, Y+Size);
glTexCoord2f(0,0); glVertex2f(X-Size, Y+Size);
glEnd;

Andreaz
13-06-2006, 12:24 PM
Another Thing I found out on the internet was the fact that I can place a maximum of 8 lights on the screen GL_Light0 to GL_Light7, is this true? and if so, I there a way to work around that?

There's actually a way, using multipass rendering, supports virtually unlimited number of lights :):

http://www.delphi3d.net/download/lightaccum.zip

Smotsholle
15-06-2006, 06:26 AM
I've changed the rules of the game a bit so I only need one light.

About the alpha layer. I managed to get it working using the first rule.
But I might be able to use the second advice for future projects.
I'll certainly keep the extra lighting tip in store too for future projects.

Smotsholle
15-09-2006, 01:23 PM
Hello everyone, I'm back again after a long time.

Had a 6 week vacation and I've been gone so much I haven't been online in that time.

Now I'm back working on this project again.
Glad to say the game itself is just about bug-free.

I've found 6 bugs now and I'm able to fix two of them.

The other 4 bugs are related (Same bug occurs in different parts of the game).

I've copied the game on a central network disk. This means everyone on this network plays the same game.
I've allowed 6 children to play this game simultaneously and starting the game didn't seem to be too much of a problem. Just encountered a slow-down because all 6 of them were loading the same images(BMP files) from a subdirectory in the game.
The real bug is file reading/writing:
There are some binary files (saved records) which store the important data (levels, highscore lists, accounts, etc....).
As they played the game it could suddenly begin to crash when they tried to read from those files or write to them (not certain about the writing though).

When creating this game I assumed that the time between opening and closing the file would be a fraction of a second. I thought this would make it nearly impossible to get any kind of bug there. Obviously I was mistaken.

The children tried it stand alone, so no delphi error, just a generic error with the "Send" and "Don't send" button (you know what I mean).


I've thought of a sollution myself, but I don't know this will work or how to implement it.
I'll show you a file read function first to explain:


function LoadFromVrgFile(filename:string):TMySaveRecord;
var myFile : File of TMySaveRecord;
tv: TMySaveRecord;
begin
AssignFile(myFile, filename);
Reset(myFile);
Read(myFile, tv);
result := tv;
CloseFile(MyFile);
end;


I thought of catching the read/write error, should it occur.
If it does, just try again (with a max of 20 times, should be enough).
If it goes wrong more than 20 times I'll assume the file itself is bad.

Is this the right way to go about things or not?
If so, is this the right way to implement this?


function LoadFromVrgFile(filename:string):TMySaveRecord;
var myFile : File of TMySaveRecord;
tv: TMySaveRecord;
i:integer;
begin
AssignFile(myFile, filename);
i := 0;
Error := True;
while &#40;i < 20&#41; AND &#40;Error&#41; do
begin
Error &#58;= False;
Reset&#40;myFile&#41;;
&#123;$I-&#125;
Read&#40;myFile, tv&#41;;
&#123;$I+&#125;
If IOResult <> 0 then
begin
Error &#58;= True;
inc&#40;i&#41;;
end
else
result &#58;= tv;
CloseFile&#40;MyFile&#41;;
end;
end;


Made this code from the top of my head, would this work?

Chebmaster
15-09-2006, 07:15 PM
1. Hmmm... Shouldn't it generate an error on Reset first...?

2. Add some Sleep()/Wait() between retries...?

3. Would FileMode work in this case, to make file access exclusive?

4. Use TFileStream in pair with Try/Except?..

Smotsholle
18-09-2006, 12:13 PM
1. I'm not sure where the error occurs. I've tested this with the compiled EXE only.
2. I've managed to put the loop on hold for a while due to the 3rd point you mentioned.
3. I'm trying the file mode thing right now. Gonna test it soon. I'll let you know how it turns out.
4. Don't really understand this one. Do you have a piece of example code?

Chebmaster
19-09-2006, 01:41 PM
uses Classes
...
Error: string;
...
function LoadFromVrgFile(filename:string):TMySaveRecord;
var
myStream : TFileStream;
i:integer;
begin
i := 0;
Error:= '';
while i < 20 do
Try
myStream:=TFileStream.Create(filename, fmOpenRead, fmShareDenyWrite);
myStream.Read(Result, SizeOf(Result));
myStream.Free;
Exit;
Except
inc (i);
Error:=(ExceptObject as Exception).Message;
Sleep(100);
End;
end;

...as you can see, streams and exceptions are immensely more elegant that the pre-historic methods you employed. :wink:

Smotsholle
19-09-2006, 09:53 PM
Hehe, I couldn't agree more.

This "prehistoric" piece of code is based on something I got off the internet. I found some basic code and copy-pasted it in my program.

The comments on it helped me understand what it does and I thought "don't mock it if it works". Well, appearantly it doesn't so you're right to mock it ;)

I'll implement this code next thursday.
Have to do this report on the project for my school and I have to make sure that people with only basic programming knowledge can understand this.
I even have to explain what a polygon is. Or a term like platform-game.

I'll let you know how this code turns out, but I have this feeling it will work :D

Smotsholle
29-09-2006, 08:13 AM
Sorry for testing this a week to late. Been ill and some other bugs came up. But I'm glad your sollution worked. I've tested this game with 5 people playing on the same network and it all performed without a single glitch.

Don't know exactly why your sollution worked, but it did, and for that I'm grateful.

I have one month left on this project now and just about every bug is fixed (some minor ones left, but I know how to solve those).

I'll post some more screenshots when it's finished.