Recent

Author Topic: BGRA Games  (Read 28001 times)

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
BGRA Games
« on: October 04, 2014, 01:24:24 am »
Hi, I've started a package called 'BGRA Games' to separate from BGRA-Controls the game specific stuff.

Web: http://bgragames.sourceforge.net/

Components:
Panel and TileMap.

- TBGPanel: like TBGRAVirtualScreen with few options to use as window
- TBGTileMap: improved TTileMap from BGRA-Controls, it has few options well organized
- Joystick support from http://wiki.freepascal.org/5dpo
- Sound support from http://wiki.freepascal.org/uos
- All the games from BGRA-Controls package

Test:
- test_bgtilemap: a WYSWYG Tile Map editor work in progress...
- test_platforms: a single level where you can move a mushroom to use HitTest over walls.

Includes two games (really a game and another version of the same).
« Last Edit: October 16, 2014, 04:25:12 am by 007 »

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: BGRA Games
« Reply #1 on: October 04, 2014, 04:40:23 pm »
Cool.

To improve DrawTile, you can do something like that (I have not checked if it works, theoretically it works but it needs to be tested):
Code: [Select]
procedure TBGTileMap.DrawTile(Bitmap: TBGRABitmap; x, y, id: NativeInt; opacity: byte);
var
  oldClip,dest,fullDest: TRect;
begin
  oldClip := Bitmap.ClipRect;
  dest := RectWithSize(x,y,FMap.TileWidth,FMap.TileHeight);
  if not IntersectRect(dest, dest, oldClip) then exit;
  Bitmap.ClipRect := dest;
  fullDest := RectWithSize(x-FRects[id].Left*FMap.TileWidth div FTileSet.TileWidth,y-FRects[id].Top*FMap.TileHeight div FTileSet.TileHeight, FTileSet.Bitmap.Width*FMap.TileWidth div FTileSet.TileWidth, FTileSet.Bitmap.Height*FMap.TileHeight div FTileSet.TileHeight);
  Bitmap.StretchPutImage(fullDest, FTileSet.Bitmap, dmDrawWithTransparency, opacity);
  Bitmap.ClipRect := oldClip;
end;
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #2 on: October 04, 2014, 09:12:05 pm »
It works! Thanks!

Try now test_platforms, is a single level where you can move a mushroom to use HitTest over walls.

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: BGRA Games
« Reply #3 on: October 05, 2014, 02:02:57 am »
I tried it. That's great!  :D

Here is some optimisation of the hit wall test:
Code: [Select]
  function BoundInt(AValue, AMin, AMax: NativeInt): NativeInt;
  begin
    if AValue <= AMin then
      result := AMin else
    if AValue >= AMax then
      result := AMax else
        result := AValue;
  end;

  function HitWall: boolean;
  var
    x,x1,x2, y,y1,y2, tx,ty, n: NativeInt;
    Player, Wall: TRectangle;
  begin
    Result := False;
    tx := TileMap.Map.TileWidth;
    ty := TileMap.Map.TileHeight;
    { Player Position and Size }
    Player := Rectangle(PlayerPosition.x, PlayerPosition.y, tx, ty);
    { Layer Data ID }
    y1 := BoundInt(Player.y div ty,0,TileMap.Map.Height - 1);
    y2 := BoundInt((Player.y+Player.Height-1) div ty,0,TileMap.Map.Height - 1);
    n := y1*TileMap.Map.Width;
    x1 := BoundInt(Player.x div tx,0,TileMap.Map.Width - 1);
    x2 := BoundInt((Player.x+Player.Width-1) div tx,0,TileMap.Map.Width - 1);
    for y := y1 to y2 do
    begin
      for x := x1 to x2 do
      begin
        { Wall Position and Size }
        Wall := Rectangle(x * tx, y * ty, tx,ty);
        { If is not empty space }
        if TileMap.Layers[0].Data[n+x] <> '-1' then
          { HitTest }
          if HitTest(Wall, Player) then
          begin
            Result := True;
            break;
          end; // hit
      end; // x
      n += TileMap.Map.Width;
    end; // y
  end;
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: BGRA Games
« Reply #4 on: October 05, 2014, 02:15:29 am »
For the bouncing to be exactly on the ground, I would suggest:
Code: [Select]
  { PlayerPosition X }
  PlayerPosition.x := PlayerPosition.x + round(AccX);

  if HitWall then
  begin
    while PlayerPosition.x <> orig.x do
    begin
      PlayerPosition.x := IntTowards(PlayerPosition.x, orig.x);
      if not HitWall then break;
    end;
    AccX := -0.2 * AccX;
  end;

  { PlayerPosition Y }
  PlayerPosition.y := PlayerPosition.y + round(AccY);

  if HitWall then
  begin
    while PlayerPosition.y <> orig.y do
    begin
      PlayerPosition.y := IntTowards(PlayerPosition.y, orig.y);
      if not HitWall then break;
    end;
    AccY := -0.2 * AccY;
  end;

Using
Code: [Select]
  function IntTowards(AValue, ATarget: integer): integer;
  begin
    if AValue < ATarget then result := AValue+1
    else if AValue > ATarget then result := AValue-1
    else result := AValue;
  end; 
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #5 on: October 05, 2014, 02:48:54 am »
Thanks! I've applied all of them.

I see, the first just calculates the surrounding area instead of the whole map. I was thinking how to achieve that, well you just did it :)
The second do that the bouncing pixel exactly on the ground. Never imagined how to do that.

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: BGRA Games
« Reply #6 on: October 05, 2014, 02:38:38 pm »
Yes, that's what it is.

It can be also faster by applying the filter at the beginning in FormCreate:
Code: [Select]
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  { Load TileMap }
  TileMap := TBGTileMap.Create('map.ini');

  { Apply Filter }
  GameBoy(TileMap.TileSet.Bitmap);
  TileMap.Map.BackgroundColor := GameBoy(TileMap.Map.BackgroundColor);

So the drawing becomes:
Code: [Select]
procedure TfrmMain.bgMapRedraw(Sender: TObject; Bitmap: TBGRABitmap);
begin
  { Draw Cache }
  Bitmap.PutImage(0, 0, CacheBitmap, dmSet);

  { Draw Player }
  TileMap.DrawTile(Bitmap, PlayerPosition.x, PlayerPosition.y, 3, 255);
end;

For this, you need a function GameBoy to apply to one TBGRAPixel:
Code: [Select]
//filter to be applied on any sequence of pixels in memory
procedure GameBoy(P: PBGRAPixel; Count: integer);
var
  c: NativeInt;
begin
  while Count > 0 do
  begin
    c := p^.red + p^.green + p^.blue;

    if c <= 382 then
    begin
      if c <= 191 then
        p^ := BGRA(0, 80, 32, p^.alpha)
      else
        p^ := BGRA(0, 104, 24, p^.alpha);
    end
    else
    begin
      if c <= 573 then
        p^ := BGRA(0, 176, 0, p^.alpha)
      else
        p^ := BGRA(112, 224, 48, p^.alpha);
    end;

    Inc(p);
    dec(Count);
  end;
end;

//filter to be applied on a bitmap, uses the generic function
procedure GameBoy(Bitmap: TBGRABitmap);
begin
  GameBoy(Bitmap.Data,Bitmap.NbPixels);
end;

//filter on one TBGRAPixel, transmit the address of the variable to the generic function
function GameBoy(c: TBGRAPixel): TBGRAPixel;
begin
  result := c;
  GameBoy(@result,1);
end;
« Last Edit: October 05, 2014, 02:45:14 pm by circular »
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #7 on: October 06, 2014, 12:21:23 am »
Thanks!

I've added uos for sound and 5dpo for joystick.

Working, just joystick doesn't compile for win64. I need to fix the compilation of test_platforms for win64. Done. The original ifdef was for win32 but works on win64 too!

Sound sometimes crashes and stop working Solved.
and sometimes gives me an error when the program is closed Only when debugging there is a small error window (sometimes)

it makes sound :)
« Last Edit: October 06, 2014, 03:33:42 am by 007 »

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #8 on: October 08, 2014, 12:33:57 am »
Now the character can die an restart the level also can get the bell (goal) and play another level.

Graphics
Levels
Joystick
Sound
Level Editor

it's finally a game that anyone can expand in their own way.
« Last Edit: October 08, 2014, 12:36:19 am by 007 »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: BGRA Games
« Reply #9 on: October 08, 2014, 10:15:45 am »
I can't help but to think that software rendering is a thing of the past for most games, in favor of graphics card support on OpenGL, DirectX or SDL. Drawing speed being comparatively 100 times slower. But still You can propably do something like simple card games. Also you'll have higher compatibility for different platforms with this.

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #10 on: October 08, 2014, 04:36:47 pm »
Yeah it's slower sometimes..

BTW the advantage I see is that we need to use caches here and there and think things twice to keep the game running faster, then the game it's highly optimized and when time comes to change the graphics engine maybe it will run faster from the start..

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: BGRA Games
« Reply #11 on: October 08, 2014, 06:12:43 pm »
Hi! I tried it. Great to have the next level.

However I get some error. In uos.pas, at line 3127, I had to add a test to avoid crash:
Code: [Select]
      if DefDevInInfo <> nil then
        DefDevInAPIInfo := Pa_GetHostApiInfo(DefDevInInfo^.hostApi);
Because DefDevInInfo is nil on my computer. Maybe it means there is no microphone?

About the background sound, it is not beautiful! Have you something more relaxing?
EDIT: Here is a background music: http://consume.o2switch.net/caves.mp3

About the joystick I could not test it as I don't have one.

Here is a patch to improve the gameplay. With this, the mushroom cannot fly anymore. It needs to jump from the floor or keep attached to the ceiling.
« Last Edit: October 08, 2014, 07:06:04 pm by circular »
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #12 on: October 08, 2014, 07:55:10 pm »
I've already changed the music just now!

The patch will not work because I've added code that is in the latest svn 35.. I've added fullscreen and for that thing some parts of the code has changed.

If you have time try it again.

Edit: UOS is still beign updated in github maybe they want to add the patch too.
« Last Edit: October 08, 2014, 07:57:05 pm by 007 »

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: BGRA Games
« Reply #13 on: October 10, 2014, 10:43:13 pm »
Since it's the first personalizable and working copy of the source code of leveloid I will release it with BGRA Games:
- Added leveloid 0.3 source code to bgra games svn

leveloid download (currently windows only):
http://sourceforge.net/projects/leveloid/

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: BGRA Games
« Reply #14 on: October 11, 2014, 01:18:59 am »
You're creative these days!  :)
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018