Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Arkanoid port (C)

  1. #1

    Arkanoid port (C)

    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

    Code:
      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.
    Last edited by Rickmeister; 30-03-2016 at 04:03 PM.

  2. #2
    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.

  3. #3
    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.
    Last edited by Ñuño Martínez; 30-03-2016 at 07:59 AM.
    No signature provided yet.

  4. #4
    PGDCE Developer de_jean_7777's Avatar
    Join Date
    Nov 2006
    Location
    Bosnia and Herzegovina (Herzegovina)
    Posts
    287
    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.
    Existence is pain

  5. #5
    Quote Originally Posted by Ñuño Martínez View Post
    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.

    Quote Originally Posted by de_jean_7777
    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...
    Last edited by Rickmeister; 30-03-2016 at 09:19 AM.

  6. #6
    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



    [edit] In case you didn't notice, the button sprites aren't colorkeyed yet It's not a bug, it's a 'feature'!
    Attached Images Attached Images
    Last edited by Rickmeister; 30-03-2016 at 02:08 PM.

  7. #7
    Quote Originally Posted by de_jean_7777 View Post
    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)

  8. #8
    Quote Originally Posted by Rickmeister View Post
    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.

  9. #9
    Quote Originally Posted by SilverWarior View Post
    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.

  10. #10
    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

Page 1 of 2 12 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
  •