ESDL question

Vlad Dumitrescu <>
Thu Oct 25 11:35:20 CEST 2001


Hi,

This question should probably go directly to Dan, but maybe others know the answer too...

I have been trying to use esdl by calling gl functions from several processes, but it doesn't work... When calling gl:swapBuffers, the process just locks... Could any wiser than me look at the code? I am not really at home with OpenGL either.

use ui:go() for a clean start. 

Thanks a lot,
Vlad

%%%----------------------------------------------------------------------
%%% File    : ui.erl
%%% Author  :  <>
%%% Purpose : 
%%% Created : 17 Oct 2001 by  <>
%%%----------------------------------------------------------------------

-module(ui).
-author('').

-compile(export_all).
%%-export([start/0, start/1, init/2, initWin/2, initWin2/2]).

-include_lib("esdl/include/sdl.hrl").
-include_lib("esdl/include/sdl_video.hrl").
-include_lib("esdl/include/sdl_keyboard.hrl").
-include_lib("esdl/include/gl.hrl").
-include_lib("esdl/include/glu.hrl").
-include_lib("esdl/include/sdl_events.hrl").

-define(DBG(Str, Args), io:format("~p/~p: " ++Str ++"~n", 
      [?MODULE, ?LINE] ++ Args)).
-define(DBG1(Str), io:format("~p/~p: " ++Str ++"~n",[?MODULE, ?LINE])).

-record(state, {root=none, wrapper, surface}).

-define(set_wrapper(X), put(sdlwrapper, X)).
-define(get_wrapper(),  element(1, get(sdlwrapper))).

go() ->
    Q=start(),
    W=initWin(600,400),
    Q!{root,W},
    W!start.

start() ->
    spawn(ui, init, [[]]).

start(Mode) when list(Mode) ->
    spawn(ui, init, [Mode]);
start(_) ->
    error.

init(Mode) ->
    register(ui, self()),

    Wrapper = sdl:init(?SDL_INIT_VIDEO bor ?SDL_INIT_ERLDRIVER),

    sdl_events:eventState(?SDL_ALLEVENTS ,?SDL_ENABLE),
    sdl_keyboard:enableKeyRepeat(100, 40),
    sdl_mouse:showCursor(false),

    Flags = 
 case lists:member(fullscreen, Mode) of 
     false ->
  ?SDL_OPENGL bor ?SDL_RESIZABLE;
     _ -> 
  ?SDL_OPENGL bor ?SDL_FULLSCREEN
 end,
    sdl_video:gl_setAttribute(?SDL_GL_DOUBLEBUFFER, 1),
    R = sdl_video:setVideoMode(640, 480, 16, Flags),

    loop(#state{wrapper=Wrapper, surface=R}).

loop(State) ->
    receive 
 {root, Desktop} ->
     loop(State#state{root=Desktop});
 {wrapper, Pid} ->
     Pid ! {wrapper, State#state.wrapper},
     loop(State);
 Msg ->
     %%io:format("cmd: ~p ~n", [Msg]),
     loop(State)
    after 1 -> 
      {_,Evs} = sdl_events:peepEvents(1, ?SDL_GETEVENT, ?SDL_ALLEVENTS),
     if length(Evs) == 1 ->
      %%io:format("event: ~p ~n", [hd(Evs)]),
      if pid(State#state.root) -> 
       State#state.root ! hd(Evs);
         true -> true
      end,
      case hd(Evs) of
   #quit{} ->
       quit();
   _ ->
       ok
      end;
        true ->true
     end,
     loop(State)
    end.

quit() ->
    ?DBG1("quitting..."),
    sdl:quit(),
    exit(normal).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

initWin(W, H) ->
    spawn(ui, initWin2, [W, H]).

initWin2(W, H) ->
    ui ! {wrapper, self()},
     receive
 {wrapper, Wrap} ->
     ?set_wrapper(Wrap),
     true
    end,
    receive 
 start -> 
     true 
    end,

    sdl_mouse:showCursor(true),

    gl:viewport(0, 0, W-1, H-1),
    gl:clearColor(0.0, 0.0, 0.0, 0.0),
    gl:clearDepth(1.0),
    gl:depthFunc(?GL_LESS),
    gl:enable(?GL_DEPTH_TEST),
    gl:shadeModel(?GL_SMOOTH), 

    gl:matrixMode(?GL_PROJECTION),
    gl:loadIdentity(),
    glu:perspective(45.0, W / H, 0.1, 100.0), 

    gl:matrixMode(?GL_MODELVIEW),

    gl:blendFunc(?GL_SRC_ALPHA, ?GL_ONE_MINUS_SRC_ALPHA),
    gl:clearColor(0, 0, 0, 1),

    %%gl:swapBuffers(),

   loop_event().

drawscene() ->
    gl:matrixMode(?GL_MODELVIEW),

    gl:clear(?GL_COLOR_BUFFER_BIT bor ?GL_DEPTH_BUFFER_BIT),
    gl:loadIdentity(),

    gl:translatef(0.0, 0.0, -6.0),

    gl:'begin'(?GL_QUADS),
    gl:color3f(0,1,0),
    gl:vertex3f(1.0, 0.0, 0.0),
    gl:color3f(1, 1,1),
    gl:vertex3f(1.0, 1.0, 0.0),
    gl:vertex3f(0.0, 1.0, 0.0),
    gl:color3f(1,0,0),
    gl:vertex3f(0.0, 0.0, 0.0),
    gl:'end'(),

    gl:'begin'(?GL_QUADS),
    gl:color3f(0,1,1),
    gl:vertex3f(1.5, 0.5, -1.0),
    gl:color3f(1, 1,1),
    gl:vertex3f(1.5, 1.5, -1.0),
    gl:vertex3f(0.5, 1.5, -1.0),
    gl:color3f(1,0,1),
    gl:vertex3f(0.5, 0.5, -1.0),
    gl:'end'(),

    gl:swapBuffers(),
    true.

loop_event() -> 
    receive 
 {'EXIT', Pid, R} ->
     io:format("Error Pid  ~p died with reason ~p ~n", [Pid, R]),
     quit;
  #quit{} -> 
      ?DBG1("quit root"),
     quit;
 Else ->
     io:format("got: ~p ~n", [Else]),
     drawscene(),
     loop_event()
 %    after 0 ->
%      %%io:format("Jerky?? Your opengl card is probably too slow ~n", []),
%      loop_event()
    end.








More information about the erlang-questions mailing list