Author Topic: 2D Entitysystem ;)  (Read 10989 times)

Offline D2O

  • Prof. Inline
  • *****
  • Posts: 1061
    • View Profile
    • http://www.deuteriumoxid.com
Re: 2D Entitysystem ;)
« Reply #15 on: 2008-Aug-23 »
This one looks interesting, how finished is it? Is there any zip-file to download?

Hi Moru, its no zip download, only the Codepasting.

I have in moment short time!?
But i think you can use the SPRITE CODE and ERROR CODE.


Puhh, man bin ich gut im English :puke:

I7 2600K; 8 GB RAM ; Win10 Pro x64 | NVidia GTX 750 TI 2048MB ; Realtec OnBoard Sound;
Lenovo ThinkPad T400: XP Pro
GLB Premium-immer Aktuell

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Re: 2D Entitysystem ;)
« Reply #16 on: 2008-Aug-24 »
Thanks, I will try to use the sprite code then, that's exactly what I needed anyway :-)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2597
    • View Profile
Re: 2D Entitysystem ;)
« Reply #17 on: 2010-Jan-19 »
Noseying about this old thread, lots missing  ;/, anyone have a link to another version?
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Re: 2D Entitysystem ;)
« Reply #18 on: 2010-Jan-19 »
I don't know if this is the sourcecode but it is saved around that time:

Code: GLBasic [Select]
// --------------------------------- //
// Project: entity2d
// Start: Tuesday, July 15, 2008
// IDE Version: 5.322




FUNCTION E_ATAN2a : l_sprite1 AS Timage,l_sprite2 AS Timage ,l_line = FALSE
        IF l_line = TRUE THEN DRAWLINE l_sprite1.x,l_sprite1.y,l_sprite2.x,l_sprite2.y,RGB(0xff, 0x00, 0x00)
        RETURN  MOD((ATAN(l_sprite2.y-l_sprite1.y,l_sprite2.x-l_sprite1.x)+360),360)
ENDFUNCTION



FUNCTION E_ATAN2 : l_x1,l_y1, l_x2,l_y2, l_line = FALSE
        IF l_line = TRUE THEN DRAWLINE l_x1,l_y1,l_x2,l_y2,RGB(0xff, 0x00, 0x00)
        RETURN  MOD((ATAN(l_y2-l_y1,l_x2-l_x1)+360),360)
ENDFUNCTION



FUNCTION E_DRAWSINCOS : l_sprite AS Timage,l_x,l_y,l_frame = 0,l_rc = 50,l_rs = 50,l_speed = 0.3,l_left_right = 0,l_angel = 359
        //STATIC l_i

        IF l_left_right = 0
                INC l_sprite.i,l_speed
                IF l_sprite.i > l_angel THEN l_sprite.i = 0
                ELSE
                        DEC l_sprite.i,l_speed
                IF l_sprite.i < 0 THEN l_sprite.i = l_angel
        ENDIF

        l_sprite.y = l_y+SIN(l_sprite.i)*l_rc
        l_sprite.x = l_x+COS(l_sprite.i)*l_rs

        E_DRAWSPRITE(l_sprite,l_sprite.x,l_sprite.y,l_frame)

//      RETURN l_sprite
ENDFUNCTION


// Zeichnet das Sprite an l_x,l_y und wird auf l_sizex,l_sizey gestretcht
//draws the sprite at 1_x,1_y and is streched to 1_sizex,1_sizey

FUNCTION E_STRETCHSPRITE AS Timage :  l_sprite AS Timage,l_x,l_y,l_sizex,l_sizey,l_frame = 0
        IF l_sprite.load = TRUE
                l_sprite.tilex = l_sizex
                l_sprite.tiley = l_sizey

                IF l_sprite.sprt = 1
                        STRETCHSPRITE l_sprite.sprite, l_x,l_y,l_sizex,l_sizey
                ELSE
                        STRETCHANIM   l_sprite.sprite,l_frame, l_x,l_y,l_sizex,l_sizey
                ENDIF
        ENDIF

        RETURN l_sprite
ENDFUNCTION


        // kopiert / Grabt ein Sprite
        //copies / grabs a sprite
FUNCTION E_GRABSPRITE AS Timage : l_x,l_y,l_width,l_high
        LOCAL temp AS Timage

        temp.sprite = E_SPRITEINDEX()
        temp.name$       = "Grabsprite"
        temp.x           = l_x
        temp.y           = l_y

        temp.tilex   = l_width

        temp.tiley   = l_high


        temp.frame       = 0
        temp.col         = 0
        temp.load    = TRUE
        temp.sprt        = 1

        GRABSPRITE temp.sprite,l_x,l_y,l_width,l_high

        RETURN temp
ENDFUNCTION




// Speichert das sprite als Datei
// saves the sprite as file
FUNCTION E_SAVESPRITE : l_path$ ,l_sprite AS Timage

        SAVESPRITE l_path$,l_sprite.sprite

ENDFUNCTION




// Sprite Drehen
//rotate the sprite
FUNCTION E_ROTOSPRITE : l_sprite AS Timage,l_x,l_y,l_angle
        IF l_sprite.load = TRUE
                ROTOSPRITE l_sprite.sprite,l_x,l_y,l_angle
        ENDIF
ENDFUNCTION


// Sprite Zoomen und Drehen, Sprite wird als Single oder Animate erkannt
// zoom and rotate the sprite, sprite is recognized as single or animate
FUNCTION E_ROTOZOOMSPRITE : l_sprite AS Timage,l_x,l_y,l_angle,l_frame = 0,l_scale = 1

        IF l_sprite.load = TRUE
        IF l_frame > l_sprite.frame THEN l_frame = 0

        IF l_sprite.sprt = 1
                ROTOZOOMSPRITE l_sprite.sprite, l_x,l_y,l_angle, l_scale
        ELSE
                ROTOZOOMANIM l_sprite.sprite,l_frame, l_x,l_y,l_angle, l_scale
        ENDIF
        ENDIF
ENDFUNCTION


// Naja, hier bin ich noch nicht ganz schlau geworden           //nya i didn&#39;t fully understood this part
//FUNCTION E_DRAWPOLY : l_sprite AS Timage ,l_frame = 0
//
// l_sprite.load = TRUE
// IF l_frame > l_sprite.frame THEN l_frame = 0
//  STARTPOLY   l_sprite.sprite // Bitmap = Nr.0
// //obenlinks  x,      y,                                                                              //upper left
//
// SATIC a,b,c,d
//                               KEY(30) AND KEY(205) THEN INC a,0.5
//                               KEY(30) AND KEY(203) THEN DEC a,0.5
//                               KEY(48) AND KEY(205) THEN INC b,0.5
//                               KEY(48) AND KEY(203) THEN DEC b,0.5
//                               KEY(46) AND KEY(205) THEN INC c,0.5
//                               KEY(46) AND KEY(203) THEN DEC c,0.5
//                               KEY(32) AND KEY(205) THEN INC d,0.5
//                               KEY(32) AND KEY(203) THEN DEC d,0.5
//  LYVECTOR  a,   b,  32,  32, RGB(0xff, 0x10, 0x80)
//  LYVECTOR   a, b+32,  32, 32, RGB(0xff, 0x10, 0x80)
//  LYVECTOR 32+a, b+32, 32, 32, RGB(0xff, 0x10, 0x80)
//  LYVECTOR 32+a,  b, 32,  32, RGB(0xff, 0x10, 0x80)
// ENDPOLY
//
// ENDIF
//ENDFUNCTION
//




// Es kann im Image intern die RGB farben abgespeichert werden / test??
// it can in the image internal save the RGB colors / test ??
FUNCTION E_SETSPRITTANS AS Timage :l_sprite AS Timage, l_r,l_g,l_b

        l_sprite.r = l_r
        l_sprite.g = l_g
        l_sprite.b = l_b


        RETURN l_sprite
ENDFUNCTION


// Erstellt einen Kreis als ring, mit l_new als False könnte es realtime genutzt werden->Test
// creates a circle as ring, with 1_new as false it could be used realtime -> test
FUNCTION E_CREATEOVALLINE AS Timage :  l_sizex,l_sizey, l_rgb = 0xffffff, l_new = TRUE
        STATIC s_index
        STATIC s_ctrl
        LOCAL l_x,l_y


        LOCAL l_i

        IF l_new = FALSE AND s_ctrl = 0
        s_index = E_SPRITEINDEX()
        s_ctrl = 1
        ENDIF

        IF l_new = TRUE
        s_ctrl = 0
        s_index = E_SPRITEINDEX()
        ENDIF

        l_sizex = l_sizex/2
        l_sizey = l_sizey/2

        LOCAL l_sprite AS Timage
        l_sprite.name$ = "E_DRAWOVALLINE"
        l_sprite.tilex = l_sizex
        l_sprite.tiley = l_sizey
        l_sprite.sprite = s_index
        l_sprite.frame = 0
        l_sprite.sprt = 1
        l_sprite.load = TRUE

        DRAWRECT l_x,l_y   ,l_sizex*2   ,l_sizey*2 +l_sizey   ,  RGB(0xff, 0x00, 0x80)

        FOR l_i = 0 TO 359
                //SETPIXEL l_x+l_sizex+SIN(l_i)*l_sizex , l_y+COS(l_i)*l_sizey  ,l_rgb
                SETPIXEL l_x+l_sizex+SIN(l_i)*(l_sizex-1) , l_y+COS(l_i)*(l_sizey-1)+l_sizey  ,l_rgb
        NEXT

        GRABSPRITE l_sprite.sprite,l_x , l_y,  l_sizex*2   ,l_sizey*2+l_sizey
        BLACKSCREEN

        RETURN l_sprite
ENDFUNCTION

// Erstellt einen Voll Kreis/oval , mit l_new als False könnte es realtime genutzt werden->Test
// creates a full circle/oval, with 1_new as false it could be used realtime -> test
FUNCTION E_CREATEOVAL AS Timage : l_sizex,l_sizey, l_rgb = 0xffffff, l_new = TRUE
        STATIC s_index
        STATIC s_ctrl
        LOCAL l_x,l_y

        LOCAL l_i

        IF l_new = FALSE AND s_ctrl = 0
                s_index = E_SPRITEINDEX()
                s_ctrl = 1
        ENDIF

        IF l_new = TRUE
                s_ctrl = 0
                s_index = E_SPRITEINDEX()
        ENDIF

        l_sizex = l_sizex/2
        l_sizey = l_sizey/2

        LOCAL l_sprite AS Timage
        l_sprite.name$ = "E_DRAWOVAL"
        l_sprite.tilex = l_sizex
        l_sprite.tiley = l_sizey
        l_sprite.sprite = s_index
        l_sprite.frame = 0
        l_sprite.sprt = 1
        l_sprite.load = TRUE

        DRAWRECT l_x,l_y   ,l_sizex*2   ,l_sizey*2 +l_sizey   ,  RGB(0xff, 0x00, 0x80)

        FOR l_i = 0 TO 359
                //SETPIXEL l_x+l_sizex+SIN(l_i)*l_sizex , l_y+COS(l_i)*l_sizey  ,l_rgb
                DRAWLINE   l_x , l_y+l_sizey   , l_x+SIN(l_i)*(l_sizex-1)+l_sizex , l_y+COS(l_i)*(l_sizey-1)+l_sizey  ,l_rgb
        NEXT

        GRABSPRITE l_sprite.sprite,l_x , l_y,  l_sizex*2   ,l_sizey*2+l_sizey
        BLACKSCREEN

        RETURN l_sprite
ENDFUNCTION



// Erstellt eine Box als linie, mit l_new als False könnte es realtime genutzt werden->Test
//creates a box as line, with 1_new as false it could be used  realtime -> test
FUNCTION E_CREATERECTLINE   AS Timage:  l_sizex,l_sizey, l_rgb = 0xffffff, l_new = TRUE
        STATIC s_index
        STATIC s_ctrl
        LOCAL l_x,l_y

        IF l_new = FALSE AND s_ctrl = 0
                s_index = E_SPRITEINDEX()
                s_ctrl = 1
        ENDIF

        IF l_new = TRUE
                s_ctrl = 0
                s_index = E_SPRITEINDEX()
        ENDIF

        LOCAL l_sprite AS Timage
        l_sprite.name$ = "E_DRAWRECTLINE"
        l_sprite.tilex = l_sizex
        l_sprite.tiley = l_sizey
        l_sprite.sprite = s_index
        l_sprite.frame = 0
        l_sprite.sprt = 1
        l_sprite.load = TRUE

        DRAWRECT l_x,l_y,l_sizex,l_sizey,l_rgb
        DRAWRECT l_x+1,l_y+1,l_sizex-2,l_sizey-2,RGB(l_sprite.r, l_sprite.g, l_sprite.b)
        GRABSPRITE l_sprite.sprite,l_x,l_y,l_sizex,l_sizey
        BLACKSCREEN

        RETURN  l_sprite
ENDFUNCTION






// Erstellt eine Box als ring, mit l_new als False könnte es realtime genutzt werden->Test
//creates a box as ring, with 1_new as false it could be used realtime -> test
FUNCTION E_CREATERECT  AS Timage:  l_sizex,l_sizey, l_rgb = 0xffffff, l_new = TRUE

        STATIC s_index
        STATIC s_ctrl
        LOCAL l_x,l_y

        IF l_new = FALSE AND s_ctrl = 0
                s_index = E_SPRITEINDEX()
                s_ctrl = 1
        ENDIF

        IF l_new = TRUE
                s_ctrl = 0
                s_index = E_SPRITEINDEX()
        ENDIF

        LOCAL l_sprite AS Timage
        l_sprite.name$ = "E_DRAWRECT"
        l_sprite.tilex = l_sizex
        l_sprite.tiley = l_sizey
        l_sprite.sprite = s_index
        l_sprite.frame = 0
        l_sprite.sprt = 1
        l_sprite.load = TRUE
        //      l_sprite.x = l_x
        //      l_sprite.y = l_y

        DRAWRECT l_x,l_y,l_sizex,l_sizey,l_rgb
        GRABSPRITE l_sprite.sprite,l_x,l_y,l_sizex,l_sizey
        BLACKSCREEN

        RETURN  l_sprite
ENDFUNCTION








// Spritecoillision für Single oder Animate Sprite
//spritecollision for single  or animate sprite

FUNCTION E_SPRCOLL : l_sprite1 AS Timage , l_x1, l_y1,l_frame1,l_sprite2 AS Timage ,l_x2,l_y2,l_frame2

        LOCAL l_out
        IF l_frame1 > l_sprite1.frame THEN l_frame1 = 0
        IF l_frame2 > l_sprite2.frame THEN l_frame2 = 0

        IF l_sprite1.load = TRUE AND l_sprite2.load = TRUE
                IF l_sprite1.sprt = 0 OR l_sprite2.sprt = 0
                        l_out = ANIMCOLL(l_sprite1.sprite,l_frame1,l_x1,l_y1,l_sprite2.sprite,l_frame2,l_x2,l_y2)
                ELSE
                        l_out = SPRCOLL(l_sprite1.sprite,l_x1,l_y1,l_sprite2.sprite,l_x2,l_y2)
                ENDIF
        ENDIF

        RETURN l_out
ENDFUNCTION






//---------------------------------------------------------------------

// Gibt die X grösse zurück
//returns the size of x
FUNCTION E_GETSPRITESIZEX :  l_sprite AS Timage
        GETSPRITESIZE l_sprite.sprite,l_sprite.tilex,l_sprite.tiley
        RETURN l_sprite.tilex
ENDFUNCTION

// Gibt die Y grösse zurück
//returns the size of y
FUNCTION E_GETSPRITESIZEY :   l_sprite AS Timage
        GETSPRITESIZE l_sprite.sprite,l_sprite.tilex,l_sprite.tiley
        RETURN l_sprite.tiley
ENDFUNCTION


// Ladet ein Single sprite
//loads a single sprite
FUNCTION E_LOADSPRITE AS Timage :  l_path$
        E_RUNTIMEERROR(l_path$)
        LOCAL l_sprite AS Timage


        l_sprite.sprite = E_SPRITEINDEX()

        LOADSPRITE l_path$,l_sprite.sprite

        GETSPRITESIZE l_sprite.sprite,l_sprite.tilex,l_sprite.tiley
        l_sprite.load = TRUE
        l_sprite.frame = 0
        l_sprite.name$ = l_path$
        l_sprite.sprt = 1

        RETURN l_sprite
ENDFUNCTION




//--------------------------------loadanimimage---------------------------
//  file Path, tile width, tile high


// Ladet ein Tileset
//loads a tileset
FUNCTION E_LOADANIM AS Timage  : l_path$,l_tilex,l_tiley
        E_RUNTIMEERROR(l_path$)

        //-------------

        LOCAL l_x,l_y                           // zum berechnen der Tile anzahl aus der Image grösse //calculate the number of tiles from the image size
        LOCAL  l_xcount,l_ycount        // Counter für die Positions verschiebung // counter for the position shifting
        LOCAL imagetemp                         // für die aufnahme des Tilsets //for the "recording" of the tileset


        LOCAL image AS Timage           //für die aufnahme der Tiles //for the "recording" of the tiles

        image.tilex = l_tilex
        image.tiley = l_tiley
        image.sprite = E_SPRITEINDEX()
        image.frame = l_frame
        image.name$ = l_path$
        image.sprt = 0
        image.load = TRUE


        LOADANIM l_path$,image.sprite,l_tilex,l_tiley
        GETSPRITESIZE image.sprite,l_x,l_y

        image.frame = INTEGER(l_x/l_tilex)*INTEGER(l_y/l_tiley)-1

        RETURN image
ENDFUNCTION


// Gibt den Speicher wieder frei, bei l_all = 1 werden alle benutzten Indexzahlen frei gegeben
//releases the memory again, if l_all = 1 all used indexnumbers are released
// nur zum Testen //only for testing
FUNCTION E_FREESPRITE : l_mem AS Timage,l_all = 0,l_indexstart = 1000
LOCAL l_temp,l_i
        IF l_all = 0
                l_mem.name$ = ""
                l_mem.x = 0
                l_mem.y = 0

                l_mem.tilex = 0
                l_mem.tiley = 0
                l_mem.frame = 0
                l_mem.col = 0
                l_mem.load = FALSE
                l_mem.sprt = 0
                LOADSPRITE "",l_mem.sprite
        ELSE
                l_temp = E_SPRITEINDEX(FALSE)-1
                FOR l_i = l_indexstart TO l_temp
                        LOADSPRITE "",l_i
                NEXT
        ENDIF

ENDFUNCTION




// Zeichnet ein Single oder Animate Sprite mit einem Alphawert
//draws a single or animate sprite with alphavalue
FUNCTION E_DRAWSPRITEALPHA AS Timage : l_sprite AS Timage,l_posx,l_posy,l_frame = 0,l_alpha = 0

        IF l_sprite.load = TRUE
                IF l_frame > l_sprite.frame THEN l_frame = 0
                IF l_frame > l_sprite.frame  // Frame out of range ? Then dummy gfx
                        l_frame = 0
                        DRAWRECT l_posx,l_posy,l_sprite.tilex,l_sprite.tiley,RGB(RND(255),50,50)
                ELSE
                        ALPHAMODE l_alpha
                        IF l_sprite.sprt = 0
                                DRAWANIM l_sprite.sprite,l_frame,l_posx,l_posy
                        ELSE
                                DRAWSPRITE l_sprite.sprite,l_posx,l_posy
                        ENDIF

                        ALPHAMODE  0
                ENDIF

        ENDIF
        RETURN l_sprite
ENDFUNCTION


// Zeichnet ein Single oder Animate Sprite
//draws a single or animate sprite
FUNCTION E_DRAWSPRITE AS Timage : l_sprite AS Timage,l_posx,l_posy,l_frame = 0
        IF l_sprite.load = TRUE
                IF l_frame > l_sprite.frame THEN l_frame = 0
                IF l_sprite.sprt = 0
                        DRAWANIM  l_sprite.sprite,l_frame,l_posx,l_posy
                ELSE
                        DRAWSPRITE l_sprite.sprite,l_posx,l_posy
                ENDIF
        ENDIF

        RETURN l_sprite
ENDFUNCTION




//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV



// Index Nr 0 to **
// Param True = inc index
// Param False = next Free index
FUNCTION E_SPRITEINDEX : l_in = TRUE, l_startindex = 1000
        STATIC s_index

        IF l_in = TRUE
                INC s_index,1
                RETURN s_index -1 + l_startindex
        ELSE
                RETURN s_index +l_startindex
        ENDIF
ENDFUNCTION



TYPE Tsprite
        x
ENDTYPE

TYPE  Timage
        name$           //Filename
        x                       //Position X
        y                       //Position Y
        tilex           //Tile width
        tiley           //Tile high
        sprite          //Sprite index
        frame           // Maximal Animation 0 -> ?
        col                     // Collision True | False
        load        // True Or false for Freeimage()
        sprt            //sprite Type | 0 = loadanimimage | 1 = loadimage
        r       = 255   // RGB color
        g       = 0
        b       = 128
        i                       //forschleife //forloop
ENDTYPE

 

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2597
    • View Profile
Re: 2D Entitysystem ;)
« Reply #19 on: 2010-Jan-19 »
Thanks for looking that out for me Moru, its a nice read!  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)