Results 1 to 9 of 9

Thread: Weird error

  1. #1

    Weird error

    Hello friends

    In order to get better at programming, I decided to follow a tutorial called "reconstructing cave story" over at https://github.com/JIghtuse/cavestor...7ea5f85a5e/src, but instead of C++, I'm trying to do it in Pascal.
    After a few hours, I've hit the first roadblock. The code compiles just fine, but when I try to run the program, I alsways get "External: SIGSEGV" and it stops here:
    Code:
    constructor sprite.sprite(filepath:string; sourcex:integer; sourcey:integer; width:integer; height:integer; ren:psdl_renderer);
    begin
        inherited;
        renderer_ := ren;
        texture_ := img_loadtexture(renderer_, pchar(filepath));
        sourcerect_.x := sourcex;
        sourcerect_.y := sourcey;
        sourcerect_.w := width;
        sourcerect_.h := height;
    end;
    This part gets called by
    Code:
    new(sprite_);
        sprite_^.sprite('gfx/MyChar.png', 0, 0, 16, 16, @graphics.getrenderer);
    Code:
    type
        sprite = class
        public
            constructor sprite(filepath:string; sourcex:integer; sourcey:integer; width:integer; height:integer; ren:psdl_renderer);
            destructor sprite();
    
            procedure draw(graphics:graphics; x:integer; y:integer);
    
        private
            renderer_: psdl_renderer;
            texture_: psdl_texture;
            sourcerect_: tsdl_rect;
        end;
    I don't really understand what I'm doing wrong. It would be great if somebody could give it a quick look over.

    Just in case, here is the full code (~200 lines of code in total).
    cave_story.lps http://pastebin.com/3mSDfgrB
    game1.pas http://pastebin.com/bCmKteK2
    graphics1.pas http://pastebin.com/iERSNVqx
    sprite1.pas http://pastebin.com/ZiPvMvCi

    Thanks a lot in advance guys.
    Last edited by much.love; 21-03-2014 at 06:29 PM.

  2. #2
    new is used to instantiate objects, not classes. For classes you need a create constructor, but you have sprite as constructor AND destructor. Both are methods that should need diferent names.
    Last edited by pitfiend; 21-03-2014 at 08:19 PM.

  3. #3
    Great! Thank you very much.
    Now the program at least doesn't crash. It still doesn't quite work as it should, but I'll get there.

  4. #4
    You welcome, the natural way to instantiate, use and release a class, is like this
    Code:
    type
       tmyclass = class
       public
          constructor create;
       end;
    
    var
       myclass: tmyclass;
    
    begin
       myclass:= tmyclass.create;
       //
       //  some processing...
       //
       myclass.free;
    end.
    As you can see, I didn't declare a free destructor, it is implicitly inherited from tobject class. If you want to inherite some other class you must specify it when creating it, like this:
    Code:
    type
       tmyclass = class(tcomponent)
       public
          constructor create;
       end;
    constructor and destructor are methods, just like any procedure, but have a fancy name and special purpouse. Inside them you must put your initialization and finalization code for the class. Of course you can inherite your own classes.
    Hope this bring you some light. Happy codding!
    Last edited by pitfiend; 22-03-2014 at 07:59 PM.

  5. #5
    Thanks again. The program is working now, but now I'm trying to implement the input handeling and FPC acts up again.
    The code is the following:
    Code:
    pressedkeys_: array of boolean;  
    
    [...]
    
    procedure input.keydownevent(event:psdl_event);
    begin
        pressedkeys_[event^.key.keysym.scancode] := true;
    end;
    
    [...]
    
    function input.waskeypressed(key:psdl_keycode): boolean;
    var
        event: integer;
    begin
        event := key^.keysym.sym;
        waskeypressed := pressedkeys_[event];
    end;
    and the part in the game loop:
    Code:
            while sdl_pollevent(@e) <> 0 do
            begin
                case e^.type_ of
                    sdl_keydown: input_.keydownEvent(e);
                end;
                if input_.waskeypressed(e) = sdlk_escape then running := false;
            end;
    The "event := key^.keysym.sym;" part in input.waskeypressed gives me "Error: Illegal qualifier". It's probably a very trivial mistake on my part, but I can't really figure it out on my own.

    Thanks again in advance for any help.

  6. #6
    The most likely problem is that you are trying to acces elements from dynamic array with no pre set lenght which means it has the lenght of 0 elements. In Delphi this would result in "Out of bonds" error but I'm not sure what error FPC would create.

    Anywhay instead of trying to duplicate C++ method of imput hndling why don't you use one from Pascal based SDL demos.
    It is not always good to directly translate source code from one programming language to another becouse not all programming languages alow using same aproaches.

  7. #7
    Thanks for your answer. I took a quick look at how hedgewars handles its input. Unfortunately, even if I set the array to [0..65536], like they do in hedgewars, the error message remains the same.
    Last edited by much.love; 24-03-2014 at 12:13 AM.

  8. #8
    array size should depend on enum range
    and if key^.keysym.sym is an integer enum or sth then you can typecast it like this: event := integer(key^.keysym.sym);

  9. #9
    Alright guys. Somehow the code works (for now). Thank you very much. I'm sure, I'll be back.

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
  •