From 86fe8aadfa4d4713fe1c38f49b39d66d317830ba Mon Sep 17 00:00:00 2001 From: plouj Date: Sun, 23 Dec 2007 01:00:59 +0000 Subject: [PATCH] drawing random pixels with random colour inside a program loop git-svn-id: http://svn.plouj.com/repos/eraytracer@2 b8c14938-b733-429d-887c-d66ed1f8cd15 --- raytracer.erl | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/raytracer.erl b/raytracer.erl index e986003..f890f56 100644 --- a/raytracer.erl +++ b/raytracer.erl @@ -2,6 +2,7 @@ -include_lib("esdl/include/sdl.hrl"). -include_lib("esdl/include/sdl_video.hrl"). -include_lib("esdl/include/sdl_events.hrl"). +%-include_lib("/home/plouj/programming/sdl_util.hrl"). -export([go/0]). go() -> @@ -17,22 +18,43 @@ go(Config) -> ?SDL_ANYFORMAT bor ?SDL_RESIZABLE end, ScreenRef = sdl_video:setVideoMode(640, 480, 32, Flags), + sdl_video:wm_setCaption(["Erlang SDL"], []), %io:format("Video Driver Name: ~s~n", [sdl_video:videoDriverName()]), Screen=sdl_video:getSurface(ScreenRef), {R1, R2, R3} = erlang:now(), random:seed(R1, R2, R3), - main_loop(), +% sdl_video:fillRect(Screen, null, sdl_video:mapRGB(Screen, 0, 128, 255)), +% io:format("pixels: ~p~n", [sdl_video:getPixels(Screen, +% #sdl_rect{x = 0, y = 0, +% w = Screen#sdl_surface.w, +% h = Screen#sdl_surface.h})]), + main_loop(Screen), timer:sleep(1000), sdl:quit(). -main_loop() -> +main_loop(Screen) -> Event = sdl_events:pollEvent(), case Event of {quit} -> done; _Else -> - timer:sleep(100), - main_loop() + putRandomPixel(Screen), + sdl_video:flip(Screen), + %timer:sleep(1), + main_loop(Screen) end. +putPixel(Surface, X, Y, {Red, Green, Blue}) -> + io:format("putting pixel ~w at ~w,~w~n",[{Red, Green, Blue}, X, Y]), + %PixelFormat = sdl_video:getPixelFormat(Surface), + %OriginalPixels = sdl_video:getPixels(Surface), + sdl_video:fillRect(Surface, + #sdl_rect{x = X, y = Y, w = 1, h = 1}, + sdl_video:mapRGB(Surface, Red, Green, Blue)). + +putRandomPixel(Surface) -> + putPixel(Surface, + random:uniform(Surface#sdl_surface.w)-1, + random:uniform(Surface#sdl_surface.h)-1, + {random:uniform(256)-1, random:uniform(256)-1, random:uniform(256)-1}). -- 2.11.4.GIT