Page 5 of 30 FirstFirst ... 3456715 ... LastLast
Results 41 to 50 of 300

Thread: Writing a better 2D engine. (Phoenix 2D Game Engine)

  1. #41
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    I've made the changes to phxImage.

    Avaialble from here: http://www.cairnsgames.co.za/files\phx-2d.zip

    Currently LOTS of overloading as it just makes coding so much easier. We could remove the overloading and then write another S2DL layer over the others with the relevant overloaded names.

    I'm not happy with the rotation stuff in phxImage as it seems to be putting the image in the wrong place.

    I will try do a conversion of my Flies game (included with the S2DL download) to use this engine instead.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  2. #42

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    Looks quite good, could use some optimisations through (Sel GLXImageList for some of em). And then some documentation

    We should change the pushAttrib aswell, there's no need to restore colors and blending to some semi- standard value as youve done now:

    glColor4f( 1.0, 1.0, 1.0, 1.0);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    instead we push theese 2 bits:

    GL_COLOR_BUFFER_BIT: for glColor etc
    GL_CURRENT_BIT : for glBlendFunc

    glPushAttrib(GL_ENABLE_BIT or GL_COLOR_BUFFER_BIT or GL_CURRENT_BIT );
    glBlendFunc (GL_SRC_ALPHA, GL_ONE);
    glColor4f(1,1,1, Alpha);
    Draw();
    glPopAttrib();

    For a full spec of glPushAttrib, see:
    http://pyopengl.sourceforge.net/docu...Attrib.3G.html
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  3. #43

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    Oh, and btw use the texture matrix for Tile draw instead :O, ie:

    [pascal]
    glBegin(GL_QUADS);
    glTexCoord2f(0,0); glVertex2i(X , Y);
    glTexCoord2f(W,0); glVertex2i(X+FWidth, Y);
    glTexCoord2f(W,H); glVertex2i(X+FWidth, Y+FHeight);
    glTexCoord2f(0,H); glVertex2i(X , Y+FHeight);
    glEnd();
    [/pascal]

    Saves alot of OpenGL calls :O
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  4. #44

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    I made the code needed for using display lists for image rendering instead

    The needed changes is the following. Note the difference between the 2 draw methods
    [pascal]

    TPHXImage = class(TObject)
    private
    FName : String;
    FWidth : Integer;
    FHeight : Integer;

    FTexture: glUint;

    // Display list for patterns
    FDisplayList : glUint;

    FPatternWidth : Integer;
    FPatternHeight: Integer;
    FPatternCount : Integer;

    procedure setPatternHeight(const Value: Integer);
    procedure setPatternWidth (const Value: Integer);
    public

    { Load the texture from a file. }
    Procedure LoadImage(FileName: String ); overload;
    procedure LoadImage(FileName: String; Stream: TStream); overload;

    // Rendering functions
    procedure Draw(X, Y: Integer; PatternIndex: Cardinal); overload;

    procedure Draw(X, Y: Integer ); overload;

    { Update the patterns<br>Need to be called after changed <code>PatternWidth</code> or <code>PatternHeight</code> is changed. }
    Procedure UpdatePatterns();

    Procedure Bind;

    { The name of the image, used to search for textures in the imagelist. }
    property Name : String read FName write FName;
    { The width of the image. }
    property Width : Integer read FWidth;
    { The height of the image. }
    property Height : Integer read FHeight;
    { The OpenGL texture ID. }
    property Texture : glUint read FTexture;
    { The OpenGL displaylist ID, contains <code>PatternCount+1</code> lists, where 0 is the full image. }
    property DisplayList: glUint read FDisplayList;

    { The pattern width. }
    property PatternWidth : Integer read FPatternWidth write setPatternWidth;
    { The pattern height. }
    property PatternHeight : Integer read FPatternHeight write setPatternHeight;
    { The number of patterns, same as <code>PatternWidth * PatternHeight</code>. }
    property PatternCount : Integer read FPatternCount;
    end;


    [/pascal]


    and

    [pascal]


    //------------------------------------------------------------------------------
    procedure TPHXImage.LoadImage(FileName: String);
    var TextureLoader: TPHXTextureLoader;
    var TextureData : TTextureData;
    begin
    TextureLoader:= TPHXTextureLoader.getDefaultLoader();

    TextureData:= TextureLoader.LoadTexture(FileName);

    FName := ExtractFileName(FileName);
    FTexture:= TextureData.Texture;
    FWidth := TextureData.Width;
    FHeight := TextureData.Height;

    TextureLoader.FreeData(TextureData);

    FPatternWidth := FWidth;
    FPatternHeight:= FHeight;
    UpdatePatterns();
    end;

    //------------------------------------------------------------------------------
    procedure TPHXImage.LoadImage(FileName: String; Stream: TStream);
    var TextureLoader: TPHXTextureLoader;
    var TextureData : TTextureData;
    begin
    TextureLoader:= TPHXTextureLoader.getDefaultLoader();

    TextureData:= TextureLoader.LoadTexture(FileName, Stream);

    FName := ExtractFileName(FileName);
    FTexture:= TextureData.Texture;
    FWidth := TextureData.Width;
    FHeight := TextureData.Height;

    TextureLoader.FreeData(TextureData);

    FPatternWidth := FWidth;
    FPatternHeight:= FHeight;
    UpdatePatterns();
    end;


    //------------------------------------------------------------------------------
    Procedure TPHXImage.UpdatePatterns();
    var numX: Integer;
    var numY: Integer;
    var X,Y : Integer;
    var Index : glUint;
    var cx, dx: glFloat;
    var cy, dy: glFloat;
    begin
    // Delete the old displaylist
    if FDisplayList <> 0 then glDeleteLists(FDisplayList, FPatternCount + 1);

    if((FWidth = 0) or (FHeight = 0)) then Exit;

    if(FWidth mod FPatternWidth <> 0) then
    raise Exception.Create('PatternWidth doesnt match the width.');

    if(FHeight mod FPatternHeight <> 0) then
    raise Exception.Create('PatternHeight doesnt match the height.');

    // Calculate the number of patterns
    numX:= FWidth div FPatternWidth;
    numY:= FHeight div FPatternHeight;
    // Calculate the number of patterns
    FPatternCount:= numX * numY;

    // Generate the displaylist and leave the 0 position for the full image
    FDisplayList:= glGenLists(FDisplayListSize + 1);

    glNewList(FDisplayList, GL_COMPILE);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 1.0); glVertex2i(0 , FHeight);
    glTexCoord2f(1.0, 1.0); glVertex2i(FWidth,FHeight);
    glTexCoord2f(1.0, 0.0); glVertex2i(FWidth,0);
    glTexCoord2f(0.0, 0.0); glVertex2i(0 , 0);
    glEnd();
    glEndList();

    dx:= glFloat(FPatternWidth ) / glFloat(FWidth );
    dy:= glFloat(FPatternHeight) / glFloat(FHeight);
    cx:=0;
    cy:=0;

    Index:=1;
    For y:= 1 to numY do begin
    For x:= 1 to numX do begin
    glNewList(FDisplayList + Index, GL_COMPILE);
    glBegin(GL_QUADS);
    glTexCoord2f(cx , cy+dy); glVertex2i(0 , FPatternHeight);
    glTexCoord2f(cx+dx, cy+dy); glVertex2i(FPatternWidth, FPatternHeight);
    glTexCoord2f(cx+dx, cy ); glVertex2i(FPatternWidth, 0);
    glTexCoord2f(cx , cy ); glVertex2i(0 , 0);
    glEnd();
    glEndList();
    inc(Index);

    cx:= cx + dx;
    end;
    cy:=cy + dy;
    end;
    end;


    //------------------------------------------------------------------------------
    procedure TPHXImage.Bind;
    begin
    glBindTexture(GL_TEXTURE_2D, FTexture);
    end;






    //------------------------------------------------------------------------------
    procedure TPHXImage.SetPatternWidth(const Value: Integer);
    begin
    IF Value = 0 then
    FPatternWidth:=Width
    else
    FPatternWidth:= Value;

    end;

    //------------------------------------------------------------------------------
    procedure TPHXImage.SetPatternHeight(const Value: Integer);
    begin
    IF Value = 0 then
    FPatternHeight:=Height
    else
    FPatternHeight:= Value;
    end;




    //------------------------------------------------------------------------------
    procedure TPHXImage.Draw(X, Y: Integer; PatternIndex: Cardinal);
    begin
    glPushAttrib(GL_ENABLE_BIT or GL_COLOR_BUFFER_BIT or GL_CURRENT_BIT );
    glEnable (GL_TEXTURE_2D);
    glEnable (GL_BLEND);
    glDisable (GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D, FTexture);

    glCallList(FDisplayList + PatternIndex + 1);

    glPopAttrib();
    end;



    //------------------------------------------------------------------------------
    procedure TPHXImage.Draw(X, Y: Integer);
    begin
    glPushAttrib(GL_ENABLE_BIT or GL_COLOR_BUFFER_BIT or GL_CURRENT_BIT );
    glEnable (GL_TEXTURE_2D);
    glEnable (GL_BLEND);
    glDisable (GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D, FTexture);

    glCallList(FDisplayList);

    glPopAttrib();
    end;

    [/pascal]

    Shouldnt be that hard to adapt to the other rendering functions
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  5. #45
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    Dont like this bit:

    [pascal] if(FWidth mod FPatternWidth <> 0) then
    raise Exception.Create('PatternWidth doesnt match the width.');

    if(FHeight mod FPatternHeight <> 0) then
    raise Exception.Create('PatternHeight doesnt match the height.');
    [/pascal]

    Because I often use sub images that do not tile exactly.

    Busy trying to understand what you did - Will try and adapt the other methods.

    Comments were started but then I ran out of time

    Tile image will be better to actually adapt the drawing to draw multiple quads with the same texture. But It should work as is - these are the only ones I didn;t test.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  6. #46

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    Hehe, the pattern modula thing is not really neded actually, no code that depends on the pattern size being a even multiplicator of the image size.

    hehe, blame lack of time, please do

    With displaylist looping for tiledraws is not that big of an issue, but dont use the Draw method for doing it, Function overhead in pascal is large enough as it is

    Oh, as a font renderer, what do you think of using
    http://www.angelcode.com/products/bmfont/
    to generate the fonts.

    And make a converter to convert the font files into a binary format instead, will save some loading time.
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  7. #47
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    Cool - so we remove the modula thing - However also notice that I've included code to just draw a subsection of the main texture anyway.

    Any BMP font builder is cool. If you do want to convert to some binary format consider these two things:
    1: Only one external resource for one font (ie include the image and the sizing info in one file)
    2: keep the facility to use normal image files also - I typically generate a font and infclude it in my games - my artist then uses his tool to modify the raw image and test it in game - this is done remotely so having to continually binarize the file info would add a lot of overhead.

    I promise to do more comments
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  8. #48

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    As a side thought, something that has always annoied the hell out of me about packages with image collections is that they don't let you have different image sizes throughout the image lib.

    Could we put a flag in that lets you either setup a pattern width/height or setup the pattern rectangles yourself? I've hacked this into just about every library that I've used. Some of the graphic files I've found in the past had all of the game graphics in one file. To re-build the game you either had to re-cut the graphics (painful) or setup your engine to allow for patterns at different points.

    Just my two cents again .

    Still working on the Lua integration, FPC has some searious issues when it comes to C library integration. The debugger freaks out a bit, and some times you have to pull a few tricks to get the methods to appear properly.

  9. #49

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    Quote Originally Posted by cairnswm
    Any BMP font builder is cool. If you do want to convert to some binary format consider these two things:
    1: Only one external resource for one font (ie include the image and the sizing info in one file)
    2: keep the facility to use normal image files also - I typically generate a font and infclude it in my games - my artist then uses his tool to modify the raw image and test it in game - this is done remotely so having to continually binarize the file info would add a lot of overhead.
    1. Yeah, that is the general idea of making the binary format, to merge the .fnt and the .tga file BMFont generates. And speed up loading as the .fnt file is a text file containing the character widhts, spacings etc.
    2. Loading font as normal images should be no problems, but the issue is the .fnt files, without it the engine wont know where to place the characters as the font generator doesn't place them in a normal grid. However, why not sending him the binary font compiler to (will basicly just be a program with an opendialog for the .fnt and the .tga file that them generates the binary font file.

    Quote Originally Posted by jdarling
    As a side thought, something that has always annoied the hell out of me about packages with image collections is that they don't let you have different image sizes throughout the image lib.

    Could we put a flag in that lets you either setup a pattern width/height or setup the pattern rectangles yourself? I've hacked this into just about every library that I've used. Some of the graphic files I've found in the past had all of the game graphics in one file. To re-build the game you either had to re-cut the graphics (painful) or setup your engine to allow for patterns at different points.

    Just my two cents again .
    Dont worry, the image library will for certain support different size images in the resource files, whatever the format will be. (GLXTreem supports this already).

    PatternWidth + PatternHeight is already supported, but making different sized patterns for each image is not supported, probably wont be either (not saying its not possible to do this yourself without modifying the image code as the displaylist is exposed just that it won't be built-in)
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  10. #50
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Writing a better 2D engine. (Phoenix 2D Game Engine)

    The phxImage already allows the drawing of any part of the drawing of any size - however these are not seen as patterns but are identified by X,Y widht and height properies - currently this also only allows normal drawing, no stretching, alpha or rotation.

    I Prefer image files that my artist can modify directly rather than having to send him extra tools. He takes the standard font file and adds effects, so it used the same chanacter sizing information.

    My prefered bitmap font builder is from www.LMNOPc.com (I currently use Bitmap Font Builder c1.8.2) - this also confiorms to creating fonts in the nehe bitmap font style (2 fonts in the same bitmap)
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Page 5 of 30 FirstFirst ... 3456715 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •