PDA

View Full Version : Arkanoid port (C)



Rickmeister
29-03-2016, 08:48 PM
This little pet-project is a port of my Arkanoid-clone from C/SDL to FreePascal/SDL2. Please keep in mind that I'm 'learning by doing' so eventually I might hit a speedbump :) Never, ever touched Pascal or any related dialect before downloading FPC a couple of days ago. Can't say that I have any solid OOP experience either, my language of choice has been C since about 1990 when I got my hand on a FredFish disc containing a free C compiler for the Commodore Amiga.

I've spent about two hours on this code, most of the time I've been searching the fpc-wiki for various information, like how to code for-loops and other basic stuff.:) The aim is to translate it to, if not 'good', 'acceptable' Object Pascal. So far only the SDL_Surface manager has been converted/translated to Pascal using SDL_Texture:s. I'll throw in a snippet for you to criticise :-[



TTextureList = record
Name: string;
id, refcount: integer;
tx: PSDL_Texture;
end;


{ TTextureManager }


TTextureManager = class(TObject)
private
class var TextureList: array of PTextureList;
class var texcount: integer;
protected
ArkGame: TArkanoid;
function IsLoaded(Name: string): integer;
procedure IncRef(tl: PTextureList);
procedure DecRef(tl: PTextureList);
procedure DeleteTexture(tl: PTextureList);
public
constructor Create(game: TArkanoid);
destructor Destroy; override;
function LoadTexture(Name: string): integer;
function GetTexture(id: integer): PSDL_Texture;
procedure ReleaseTexture(id: integer);
end;


{ TArkanoid is the 'main' game class }

Coming from a procedural language (C) I still have a lot to learn about how and when to use OOP to become a more efficent coder. I've tried C++ but even if it's a 'evolution' of C I find myself lost in the syntax, the fpc object pascal syntax feels more right for me. I've encorporaated a few C++ ideas though, like RAII. Not sure if this is applicable in Pascal, but unless anyone tells me not to do so, I'll continue with it.

[edit] Changed the snippet to be closer to the real code.

Rickmeister
30-03-2016, 12:22 AM
Well.. Most of the source in that snippet is already outdated :) On the plus side is that it works alot better. Decided to use dynamic arrays to store textures and changed a few lines to avoid dereferencing nil pointers when trying to access deleted textures.

Ñuño Martínez
30-03-2016, 07:56 AM
I think that's a bad idea: " destructor Destroy; reintroduce;". You should use "DESTRUCTOR Destroy; OVERRIDE;". Destructor should be virtual always. Note that compiler might do some magic with constructor and destructor (I.e. calling parent constructor or destructor if you forgot to call them). I'm not sure if reintroducing the destructor broke that magic, but I don't like to play that way.

BTW, good idea to use ARRAY instead of pointer to store lists. Pointers can be used in Pascal almost like in C, but dynamic arrays are much better.

de_jean_7777
30-03-2016, 08:12 AM
For someone getting introduced to a language it's ambitious, but then again that's how I would've done it. First get it functional, then you can improve on it. Not sure what the rest of the code is but I see a

PArkanoid = ^TArkanoid; in the code, and this could probably also be replaced by a class. If it's already a class, then you don't need the PArkanoid type, as a object reference is already a pointer of sorts, so ArkGame can be of type TArkanoid, just assign it your main TArkanoid instance.

Rickmeister
30-03-2016, 08:59 AM
I think that's a bad idea: " destructor Destroy; reintroduce;". You should use "DESTRUCTOR Destroy; OVERRIDE;". Destructor should be virtual always. Note that compiler might do some magic with constructor and destructor (I.e. calling parent constructor or destructor if you forgot to call them). I'm not sure if reintroducing the destructor broke that magic, but I don't like to play that way.

BTW, good idea to use ARRAY instead of pointer to store lists. Pointers can be used in Pascal almost like in C, but dynamic arrays are much better.

I thought that 'reintroduce' was roughly the same until I read the wiki about it, so it's already changed :) I'm still learning the syntax and I do get alot of errors when I forget to use ':=' when assigning, or '==' when comparing, but overall I do see that Pascal has it's advantages.


For someone getting introduced to a language it's ambitious, but then again that's how I would've done it. First get it functional, then you can improve on it. Not sure what the rest of the code is but I see a
Code:
PArkanoid = ^TArkanoid;
in the code, and this could probably also be replaced by a class. If it's already a class, then you don't need the PArkanoid type, as a object reference is already a pointer of sorts, so ArkGame can be of type TArkanoid, just assign it your main TArkanoid instance.

That was added just to the snippet just so it would be clear what the 'ArkGame: PArkanoid' field was refering to, not in actual source :) Again, the wiki told me that TArkanoid is already a pointer to a class object, so it's left out in the 'real' code. 'Learning by doing' produces alot of interesting errors, warnings and hints...

Rickmeister
30-03-2016, 02:02 PM
Ironed out a few wrinkles and now I've ported most of the GUi code to Pascal, when I feel that it's more or less 'complete' I'll share it with you all on github.

The GUI is quite simple, and the screenshot shows the main menu screen with a few menu items to choose between - From the center and out it's Play, Home/Exit, Highscore/Sound. The original game was controlled by gamepad (and keyboard as a fallback), and ran fine on a Raspberry Pi3 with a PS3 controller attached. By the looks of it SDL2 supports force feedback, so I'll try to 'extend' the Pascal version with that.

Haven't fiddled with supporting controllers yet, so for now It's keyboard controls only. The GUI buttons 'rotate' right and left and the selected option is always centered, scaled and highlighted. Looks more advanced then it really is :) Running a quite fresh install of Fedora so I haven't got any video-capture software to show it in action, will fix that later (when the GUI is more complete).

The game always run in '½HD' resolution (640*360) and is scaled to 720p or 1080p, I like the 'blocky' look it gets then. The C version only supports fullscreen mode on the RPi (HDMI, 720p and 1080p), and my son is currently the leaderboard champion ;)

http://www.pascalgamedevelopment.com/attachment.php?attachmentid=1402&stc=1

[edit] In case you didn't notice, the button sprites aren't colorkeyed yet :) It's not a bug, it's a 'feature'!

Rickmeister
31-03-2016, 01:42 PM
For someone getting introduced to a language it's ambitious, but then again that's how I would've done it. First get it functional, then you can improve on it.
I belive it to be a great first project, atleast when you like me have about 20-25 years of experience coding demos and games. Most of the gamecode is nothing but linked lists, sorting algorithms and database management. To say that I have zero to non experience in OOP is actually a bit of a lie, you can use abstraction, data hiding and rudimentary inheritance in C aswell, but because of the procedural nature of C you have to write alot of boilerplate code to support this design.

The big thing about larger projects is keeping them organized, if I look at the spaghetti-code I wrote back in the early 90;ies (lots of goto;s, hacking pointers, two-character variable names..) I can say that I have improved. Not that I'm any better at writing code today, but I'm doing it in a much more organized way. I'm never going to be a professional developer, I've passed the big Four-O and me and my son is writing games just for fun (I do the coding, he plays... so far, got him interested so he's hacking in Python right now)

SilverWarior
31-03-2016, 03:38 PM
I'm never going to be a professional developer, I've passed the big Four-O and me and my son is writing games just for fun (I do the coding, he plays... so far, got him interested so he's hacking in Python right now)

If you think that because you haven't managed to become professional developer till now you don't have a chance to become one then you are gravely mistaken. It is true that the chance for you to be employed by some programming company is a blit lower than for some younger people. But this is mostly because generally younger people are able to follow the fast technological advancements much easier but there are always exceptions.
For instance I have a friend of mine (actually father of one of my friends) which started learning about programming in Object Pascal about five years ago at age of 55. Now he is employed at one of the smaller development companies as assistant programmer and there are rumors that he might be taking over an entire existing project because current project leader will start working on a whole new project.
Now the biggest irony is that it is I who have introduced him into programming and he is now after about five years of learning he is earning money from this while I still hasn't earned a cent from programming despite the fact that I started learning of programming about 15 years ago (self taught). Main reason for this is probably the fact that with my projects I'm targeting big (don't have enough knowledge to finish them) and I'm not actively searching employment in this area since I believe that I still lack some needed knowledge.
I might be underestimating myself but hey risking my current stable employment (not even in IT sector) currently seems to great of a risk because it could lead to personal financial crisis if things don't work out. That is probably why I'm targeting big as that might bring me some reserve capital so in case if getting into IT sector does not work out I still have some time to get back on my feet.

As for teaching your son of programming. Have you ever tried game Colobot (https://colobot.info/colobot-gold-edition/)
This game is intended to help children learn about programming basics through gameplay. The programming language that it is teaching is C++
One day (if no one beats me) I'm planning to make similar game that would be teaching programming in Object Pascal. If Colobot would be teaching programming in Object Pascal I would probably spent countless hours playing it. But since it is teaching C++ I'm not enjoying it since C++ based and most similar syntaxes are giving me headaches even thou I first started programming in C++ itself. TO me Object Pascal syntax feels almost self explanatory.

Rickmeister
31-03-2016, 09:02 PM
If you think that because you haven't managed to become professional developer till now you don't have a chance to become one then you are gravely mistaken. It is true that the chance for you to be employed by some programming company is a blit lower than for some younger people. But this is mostly because generally younger people are able to follow the fast technological advancements much easier but there are always exceptions.

Maybe I should have said that I don't aim to become a pro :) I'm fine with beeing a hobbyist - programming is my way of relaxing. I have absolutely no intentions of making money out of my hobby. If I wanted to I guess it would be possible.

About Colobot - thanks! Will investigate it further. He started learning in a LOGO inspired script lanugage, inhouse dialect with Swedish keywords. Using that he had the ability to script 'scenes' in a point-and-click adventure I made for him. Not that advanced, but he was able to add 'clickable' items, puzzles, npc actions and dialogs with it. Never really finished that, so we went on to Python instead. We've passed linked lists, stacks and queues right now, so he's getting quite a little coder - will introduce him to pygame soon.

Rickmeister
30-12-2016, 03:33 AM
Long time since my last visit.. This project hasn't been abandoned (yet), and sit's on a USB stick somewhere :) Got the game 'playable', but eventually I lost interest, I was to be honest reinventing the wheel all over again, as I already had a working 2D game engine with a C code base, a fairly complete Entity-Component creation. In the end a I took another approach at this and just linked the Pascal code with parts of the existing C library, there by giving up on SDL2 and other improvements.

Still hacking in Pascal, and I'm now a good 5 months into my next project, but I'll keep it a secret until I actually have a almost finished product to show :)

Ñuño Martínez
30-12-2016, 09:48 AM
Good to know.

Anyway, go Pascal instead of Object Pascal. May be that will make things more easy.

Rickmeister
03-02-2017, 10:19 PM
Been trashing away at the keyboard for about 5 months now and I'm happy to show you the end result :)

http://www.pascalgamedevelopment.com/attachment.php?attachmentid=1438&stc=1


To be honest, that's alot more going on i the background than just simple Pong-clone (and yes, that's a sheep acting as ball). Instead of porting my C game engine (used in some unreleased titles like "Captain Asscrack" - a Gauntlet ripoff with a pirate twist and "Will's Revenge" - a 2D top-down adventure game with some Zelda influences) I've done a complete new 2D game engine, with support for 3D in the near future. My intentions are to release the source under the WTFPL-license (http://www.wtfpl.net/), but it's far from ready for that. Chances are that it will just sit on my harddrive and I'll go work on version 2 of the engine :)

Ñuño Martínez
04-02-2017, 05:40 PM
Keeping up the good work. :)

Rickmeister
06-02-2017, 07:59 PM
I find it fun to learn new things, and my technique to do it is perfected over 3 decades: Learn by doing things wrong a couple of times, eventually you will get it right!

Still ironing out a few quirks and wrinkles, and I only regret that I didn't make the final move to Pascal years ago. I'm actually finding it extremely fun to code again, in the same way I felt 30-some years ago when I started learning 6502 assembler on my C64. In my own humble opinion I'm a quite good C programmer, and I'm very much able to write code that both works well and looks nice, but in Pascal I achieve that with about half the effort. Actually not sure that the pascal code I write is any good, but it's nice to look at :)

My goal for now is a remake of Syndicate - one of my all time favourites. Still got the original floppy, but last time I tried to run it in dos-box it ran hyper-fast, probably because in 1993 a 60Mhz pentium was considered faster then lightning. Tried emulating a 33Mhz 486, but it's all jerky and crashes now and then.