PDA

View Full Version : Weird error



much.love
21-03-2014, 01:13 PM
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/cavestory-sdl2/tree/10c05cf13765fd74fa90c891863c047ea5f85a5e/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:

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

new(sprite_);
sprite_^.sprite('gfx/MyChar.png', 0, 0, 16, 16, @graphics.getrenderer);


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.

pitfiend
21-03-2014, 08:16 PM
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.

much.love
21-03-2014, 09:03 PM
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. :)

pitfiend
22-03-2014, 07:53 PM
You welcome, the natural way to instantiate, use and release a class, is like this

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:


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!

much.love
23-03-2014, 09:57 PM
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:


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:

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.

SilverWarior
23-03-2014, 10:53 PM
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.

much.love
23-03-2014, 11:55 PM
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.

laggyluk
24-03-2014, 12:14 AM
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);

much.love
24-03-2014, 12:56 AM
Alright guys. Somehow the code works (for now). Thank you very much. I'm sure, I'll be back. :)