PDA

View Full Version : JEDI-Sdl + dglOpenGL crash on Windows



wesz
03-12-2007, 03:50 PM
Hello, i have problem with my engine class. When i compile it on linux, all works fine, but on windows something crash application. I use FPC 2.0.4.


unit engine;

interface

uses
sdl,
dglOpenGL,
sysUtils;

var
oldGetTicks: uInt32;
frame: double;
frameRate: integer = 0;
frameCount: integer = 0;
timerTime: uInt32 = 0;

type
TEngine = class
private
quit: boolean;
event: tsdl_event;
mousePos: TPoint;

procedure keyDown(key: integer);
procedure keyUp(key: integer);
procedure mouseUp(key: integer);
procedure mouseDown(key: integer);
procedure update;
procedure render;
procedure start;
public
width, height, bpp: integer;
fullscreen: boolean;
fps, maxFps: integer;
title: string;
screen: psdl_surface;
constructor create;
procedure init;
end;

procedure initOpenGL(width, height, fps: integer);

implementation

procedure initOpenGL(width, height, fps: integer);
begin
glShadeModel(gl_smooth);
glClearColor(0.0, 0.0, 0.0, 0.0);
glDisable(gl_depth_test);
glHint(gl_perspective_correction_hint, gl_nicest);

glViewport(0, 0, width , height);

glMatrixMode(gl_projection);

glLoadIdentity();

glOrtho(0, width, height, 0, 0, fps);
glMatrixMode(gl_modelview);
glLoadIdentity();
glEnable(gl_texture_2d);
glBlendFunc(gl_src_alpha, gl_one_minus_src_alpha);
end;

constructor TEngine.create;
begin
quit := false;
fullscreen := false;
width := 800;
height := 600;
bpp := 32;
fps := 0;
maxFps := 100;
title := 'engine';
end;

procedure TEngine.init;
var
videoInfo : psdl_videoInfo;
videoFlags: cardinal;
begin
if sdl_init(sdl_init_video) = -1 then
begin
sdl_quit;
halt(1);
end;

videoInfo := sdl_getVideoInfo;

videoFlags := sdl_opengl;
videoFlags := videoFlags or sdl_doublebuf;
videoFlags := videoFlags or sdl_hwpalette;

if fullscreen then videoFlags := videoFlags or sdl_fullscreen;

if videoInfo.hw_available <> 0 then
videoFlags &#58;= videoFlags or sdl_hwsurface
else
videoFlags &#58;= videoFlags or sdl_swsurface;

if videoInfo.blit_hw <0>= timerTime + 1000 then
begin
fps &#58;= frameCount;
frameCount &#58;= 0;
timerTime &#58;= sdl_getTicks;
end;

sdl_wm_setCaption&#40;PChar&#40;'FPS&#58; ' + intToStr&#40;fps&#41;&#41;, nil&#41;;
end;

procedure TEngine.start;
var
i&#58; integer;
begin
oldGetTicks &#58;= sdl_getTicks;
frame &#58;= 0;

while not quit do
begin
while sdl_pollEvent&#40;@event&#41; > 0 do
begin
if event.type_ = sdl_quitev then quit &#58;= true;

if event.type_ = sdl_keydown then
begin
case event.key.keysym.sym of
sdlk_escape&#58;
quit &#58;= true;
end;

keyDown&#40;event.key.keysym.sym&#41;;
end;

if event.type_ = sdl_keyup then
begin
keyUp&#40;event.key.keysym.sym&#41;;
end;

if event.type_ = sdl_mousemotion then
begin
mousePos.x &#58;= event.button.x;
mousePos.y &#58;= event.button.y;
end;

if event.type_ = sdl_mousebuttonup then
begin
if event.button.button = sdl_button_left then
begin
mouseUp&#40;1&#41;;
end;
if event.button.button = sdl_button_right then
mouseUp&#40;2&#41;;
end;
if event.type_ = sdl_mousebuttondown then
begin
if event.button.button = sdl_button_left then
begin
mouseDown&#40;1&#41;;
end;
if event.button.button = sdl_button_right then
mouseDown&#40;2&#41;;
end;
end;

if sdl_getTicks - oldGetTicks > 0 then frame &#58;= frame + &#40;maxFps / &#40;1000 / &#40;sdl_getTicks - oldGetTicks&#41;&#41;&#41;;
oldGetTicks &#58;= sdl_getTicks;

for i &#58;= 1 to trunc&#40;frame&#41; do
begin
update;
end;

frame &#58;= frame - trunc&#40;frame&#41;;

render;

sdl_flip&#40;screen&#41;;

sdl_delay&#40;1&#41;
end;
end;

end.

ttp://pordesign.eu/public_html/hax/opengl.rar - sample for windows.

pstudio
03-12-2007, 05:54 PM
have you remembered to call the dglOpengl init function before usgin opengl calls?
I know I had some few headaches last time I used dglOpengl because I forgot it.

waran
03-12-2007, 07:41 PM
InitOpenGL;
ReadExtensions;


before you do anything

wesz
03-12-2007, 08:25 PM
Doesn't change anything, crash only on windows. I am confused...

arthurprs
04-12-2007, 03:01 AM
repost you code outside [C0de] tags,
the board meshed your code

wesz
04-12-2007, 07:19 AM
http://pordesign.eu/opengl.rar

Well, here is the package with source code, posting code outside [c0de] tag is uber unreadable:D So, imho package is the best way :>

Robert Kosek
04-12-2007, 12:47 PM
Wesz, edit your first post and check "Disable HTML in this post" and then repaste your code within the code tags--it'll work just fine then. ;)

arthurprs
04-12-2007, 02:51 PM
i found the problem, you are not calling InitOpenGL from dglOpenGL.

as you are using this same procedure name for the engine, just use "dglOpenGl."

put
dglOpenGL.InitOpenGL();
dglOpenGL.ReadExtensions();
at the beginning of the procedure
procedure initOpenGL(width, height, fps: integer);

wesz
04-12-2007, 04:29 PM
I have changed initOpenGL to initPorGL and added two calling lines.

procedure initPorGL&#40;width, height, fps&#58; integer&#41;;
begin
dglOpenGL.InitOpenGL&#40;&#41;;
dglOpenGL.ReadExtensions&#40;&#41;;
glShadeModel&#40;gl_smooth&#41;;
glClearColor&#40;0.0, 0.0, 0.0, 0.0&#41;;
glDisable&#40;gl_depth_test&#41;;
glHint&#40;gl_perspective_correction_hint, gl_nicest&#41;;

glViewport&#40;0, 0, width , height&#41;;

glMatrixMode&#40;gl_projection&#41;;

glLoadIdentity&#40;&#41;;

glOrtho&#40;0, width, height, 0, 0, fps&#41;;
glMatrixMode&#40;gl_modelview&#41;;
glLoadIdentity&#40;&#41;;
glEnable&#40;gl_texture_2d&#41;;
glBlendFunc&#40;gl_src_alpha, gl_one_minus_src_alpha&#41;;
end;

Ofcourse problem with engine is only on windows. When i run application:

An unhandled exception occurred at $00000000 &#58;
EAccessViolation &#58; Access violation
$00000000

http://pordesign.eu/public_html/hax/sdlgltest.rar Here is full source code and binaries for windows and linux.

WTF?

arthurprs
04-12-2007, 06:30 PM
replace


sdl_flip&#40;screen&#41;;

with


SDL_GL_SwapBuffers;

wesz
04-12-2007, 10:47 PM
Hax :* Thanks guys.

wesz
05-12-2007, 09:15 PM
Sorry for double post. But i have one more question. When i change display method on sdl_opengl, my ttf unit doesn't work.


unit fonts;
interface

uses
sdl in 'lib/sdl.pas',
sdl_image in 'lib/sdl_image.pas',
sdl_ttf in 'lib/sdl_ttf.pas',
classes,
sysUtils;

type
TFonts = class
private
fOffset &#58; tsdl_rect;
fFont &#58; pttf_font;
fFontColor &#58; tsdl_color;
fFontSurface &#58; psdl_surface;
fScreen &#58; psdl_surface;
fAlign &#58; integer;
function getX &#58; integer;
function getY &#58; integer;
procedure setX&#40;ax &#58; integer&#41;;
procedure setY&#40;ay &#58; integer&#41;;
public
property x &#58; integer read getX write setX;
property y &#58; integer read getY write setY;
property offset &#58; tsdl_rect read fOffset write fOffset;
property fontColor &#58; tsdl_color read fFontColor write fFontColor;
property align &#58; integer read fAlign write fAlign;

constructor create&#40;aScreen &#58; psdl_surface&#41;;
procedure loadFont&#40;aFile &#58; string; aSize &#58; integer&#41;;
procedure drawText&#40;Atext &#58; string&#41;;
end;

const
ALIGN_LEFT = 1;
ALIGN_CENTER = 2;
ALIGN_RIGHT = 3;

implementation

uses globals;

function TFonts.getX&#58; integer;
begin
result &#58;= fOffset.x;
end;

function TFonts.getY&#58; integer;
begin
result &#58;= fOffset.y;
end;

procedure TFonts.setX&#40;ax&#58; integer&#41;;
begin
fOffset.x &#58;= ax;
end;

procedure TFonts.setY&#40;ay&#58; integer&#41;;
begin
fOffset.y &#58;= ay;
end;

constructor TFonts.create&#40;aScreen&#58; psdl_surface&#41;;
begin
fScreen &#58;= aScreen;
fAlign &#58;= ALIGN_LEFT;
fontColor.r &#58;= 255;
fontColor.g &#58;= 255;
fontColor.b &#58;= 255;

if ttf_init < 0 then writeln&#40;'Could not load font'&#41;;
end;

procedure TFonts.loadFont&#40;aFile&#58; string; aSize&#58; integer&#41;;
begin
fFont &#58;= ttf_openFont&#40;pChar&#40;aFile&#41;, aSize&#41;;
end;

procedure TFonts.drawText&#40;aText&#58; string&#41;;
var
_tempOffset &#58; tsdl_rect;
begin
fFontSurface &#58;= ttf_renderText_solid&#40;fFont, pChar&#40;aText&#41;, fFontColor&#41;;

case fAlign of
ALIGN_LEFT&#58;
_tempOffset.x &#58;= 0;
ALIGN_CENTER&#58;
_tempOffset.x &#58;= -fFontSurface^.w div 2;
ALIGN_RIGHT&#58;
_TempOffset.x &#58;= -FFontSurface^.w;
end;

_tempOffset.x &#58;= _tempOffset.x + fOffset.x;
_tempOffset.y &#58;= fOffset.y;


sdl_blitSurface&#40;fFontSurface, nil, fScreen, @_tempOffset&#41;;
sdl_freeSurface&#40;fFontSurface&#41;;
end;

end.

I am pwned by opengl :)

savage
06-12-2007, 12:18 AM
If you are using OpenGL with SDL_ttf, check out the OpenGL/SDL_ttf demo that is part of the beta. That will show you how to use it with OpenGL.

The latest beta and demos can be found at http://jedi-sdl.pascalgamedevelopment.com

arthurprs
06-12-2007, 02:24 AM
I have made my own bitmap font engine with help of "FontStudio 4.1"