Main forum > Competitions

GLBasic Contest 02: 2D Art

<< < (3/7) > >>

dreamerman:
I had no idea for something small & simple and didn't want to copy another plasma/fire example so I made this, nothing special, just to have more entries in competition.  Code isn't piece of art, so.. ;)

Main source:

--- Code: (glbasic) ---// --------------------------------- //
// Project: 2dpixart
// by dreamerman_pl
// only GLBasic code
// coded to run in 640x480
//

TYPE point2d
    x%
    y%
ENDTYPE
TYPE point_struct
    pos AS point2d
    vec AS point2d
    target AS point2d
    dir_num%
    color%
ENDTYPE
   
   
   
GLOBAL my_point[] AS point_struct
GLOBAL my_point_count% = 1000       // how many moving rectangles
GLOBAL endit% = 0
GLOBAL i1%, anim_pos%, txt_pos AS point2d
DIM my_point[my_point_count%]

SMOOTHSHADING FALSE         // prefer stronk pixels
LIMITFPS 60
CREATESCREEN 0, 1, 640, 480
prepare_text()



// set some points
FOR i1% = 0 TO my_point_count%  -1
    my_point[i1%].pos = ret_p2d(RND(639), RND(479))
    find_newtarget(i1%)
    my_point[i1%].color% = RGB(RND(4)*64-1, RND(4)*64-1, RND(4)*64-1)   // yeap some may be totally black :D
NEXT


REPEAT
    CLEARSCREEN
    USESCREEN 0
    ALPHAMODE -0.025
    DRAWRECT 0, 0, 640, 480, RGB(0,0,0)
    ALPHAMODE -0.25
   
    // move rectangles
    FOR i1% = 0 TO my_point_count% - 1
        IF (my_point[i1%].pos.x% = my_point[i1%].target.x%)
            IF (my_point[i1%].pos.y% = my_point[i1%].target.y%)
                find_newtarget(i1%)
            ELSE
                INC my_point[i1%].pos.y%, my_point[i1%].vec.y%
            ENDIF
        ELSE
            INC my_point[i1%].pos.x%, my_point[i1%].vec.x%
        ENDIF
        DRAWRECT my_point[i1%].pos.x%-3, my_point[i1%].pos.y%-3, 7, 7, my_point[i1%].color%
    NEXT
   
    USESCREEN 1
    ALPHATESTING 1.0
    ALPHAMODE -1.0
    // text background animation, what a crap code :D
    INC anim_pos%, 2
    IF anim_pos% > 500 THEN anim_pos% = anim_pos% - 180
    FOR i1% = 0 TO 800 STEP 5
        DRAWRECT i1%, 0, 5, 200, RGB(128*ABS(SIN(i1%+anim_pos%)), 0, 255*ABS(SIN(i1%)))
    NEXT
   
    ALPHAMODE 0.0
    glEnable(GL_BLEND)                      // change opengl blend func
    glBlendFunc(GL_ONE, GL_SRC_ALPHA)       // so we can draw magenta-pink and transparent pixel at same time
    DRAWSPRITE 3, 0, 0
   
    ALPHATESTING 1.0
    USESCREEN -1
    ALPHAMODE 0.0
    DRAWSPRITE 1, 0, 0                  // draw offscreen buffer with moving rectangles
    glBlendFunc(GL_SRC_ALPHA, GL_ONE)
    glDisable(GL_BLEND)                 // back to normal?
    //CLEARSCREEN RGB(0, 255, 0)        // for tests
    ALPHATESTING 1.0                    // draw magenta-pink as transparent
    ALPHAMODE -1.0
    DRAWSPRITE 2, txt_pos.x%, txt_pos.y%    // draw text inside
    DRAWSPRITE 4, txt_pos.x%, txt_pos.y%    // text border
    SHOWSCREEN
UNTIL endit% = 1

END

// return point2d type
FUNCTION ret_p2d AS point2d: x%, y%
    LOCAL temp AS point2d
    temp.x% = x%
    temp.y% = y%
    RETURN temp
ENDFUNCTION

// find new target-direction for rectangle
FUNCTION find_newtarget%: id%
    my_point[id%].target = ret_p2d(RND(639), RND(479))
    IF (my_point[id%].target.x% < my_point[id%].pos.x%); my_point[id%].vec.x% = -1
    ELSEIF (my_point[id%].target.x% > my_point[id%].pos.x%); my_point[id%].vec.x% = 1
    ELSE; my_point[id%].vec.x% = 0
    ENDIF
    IF (my_point[id%].target.y% < my_point[id%].pos.y%); my_point[id%].vec.y% = -1
    ELSEIF (my_point[id%].target.y% > my_point[id%].pos.y%); my_point[id%].vec.y% = 1
    ELSE; my_point[id%].vec.y% = 0
    ENDIF
ENDFUNCTION

// load image from Data section and draw with desired colors
FUNCTION prepare_txt_sprite%: screenid%, acode1%, color1%, acode2%, color2%
    LOCAL i1%, i2%, i3%, i4%, i5%, czs1$
   
    RESTORE napis_glb
    READ i1%, i2%
    USESCREEN screenid%
    ALPHATESTING 1.0            // force drawing all colors even transparent magenta-pink
    ALPHAMODE 0.0
    CLEARSCREEN 0x008000ff    // transparent color
    FOR i4% = 0 TO i2% - 1
        READ czs1$
        FOR i3% = 0 TO i1% - 1
            i5% = ASC(czs1$, i3%)
            IF (i5% = acode1%)  // => if X
                DRAWRECT i3% * 4, i4% * 4, 4, 4, color1%     // black border
            ELSEIF (i5% = acode2%)  // => if :
                DRAWRECT i3% * 4, i4% * 4, 4, 4, color2%     // transparent inside
            ENDIF
        NEXT
    NEXT
    ALPHATESTING 1.0
    ALPHAMODE 0.0
ENDFUNCTION


// forcing 4x scale
FUNCTION prepare_text%:
    LOCAL i1%, i2%, i3%, i4%, czs1$, czs2$, pix%[]

    RESTORE napis_glb
    READ i1%, i2%   // get size
    txt_pos = ret_p2d((640 - i1% * 4) / 2, (480 - i2% * 4) / 2)     // calc position
    CREATESCREEN 1, 2, i1% * 4, i2% * 4     // text effect
    CREATESCREEN 2, 3, i1% * 4, i2% * 4     // text pink outside
    CREATESCREEN 3, 4, i1% * 4, i2% * 4     // text black border

    // prepare text sprites
    prepare_txt_sprite(2, 88, 0x00000000, 58, 0x00000000)
    prepare_txt_sprite(3, 88, 0x00000000, 58, 0x008000ff)
    // now we can free those surfaces
    CREATESCREEN 2, -1, 0, 0
    CREATESCREEN 3, -1, 0, 0

    USESCREEN -1    // back to back buffer
ENDFUNCTION



STARTDATA napis_glb:

// Ascii text made with: www.patorjk.com/software/taag/
// used font: Doh
DATA 126, 16
DATA "       XXXXXXXXXXXXXXXXXXXXXXXX              XXXXXXXXXXXXXXXXX                                      XXXX                      "
DATA "     XXX::::::::::::XX:::::::::X            X::::::::::::::::X                                     X::::X                     "
DATA "   XX:::::::::::::::XX:::::::::X            X::::::XXXXXX:::::X                                     XXXX                      "
DATA "  X:::::XXXXXXXX::::XXX:::::::XX            XX:::::X     X:::::X                                                              "
DATA " X:::::X       XXXXXX  X:::::X                X::::X     X:::::X  XXXXXXXXXXXXX      XXXXXXXXXX   XXXXXXX     XXXXXXXXXXXXXXXX"
DATA "X:::::X                X:::::X                X::::X     X:::::X  X::::::::::::X   XX::::::::::X  X:::::X   XX:::::::::::::::X"
DATA "X:::::X                X:::::X                X::::XXXXXX:::::X   XXXXXXXXX:::::XXX:::::::::::::X  X::::X  X:::::::::::::::::X"
DATA "X:::::X    XXXXXXXXXX  X:::::X                X:::::::::::::XX             X::::XX::::::XXXX:::::X X::::X X:::::::XXXXXX:::::X"
DATA "X:::::X    X::::::::X  X:::::X                X::::XXXXXX:::::X     XXXXXXX:::::X X:::::X  XXXXXX  X::::X X::::::X     XXXXXXX"
DATA "X:::::X    XXXXX::::X  X:::::X                X::::X     X:::::X  XX::::::::::::X  XX:::::XX       X::::X X:::::X             "
DATA "X:::::X        X::::X  X:::::X                X::::X     X:::::X X::::XXXX::::::X    XX:::::XX     X::::X X:::::X             "
DATA " X:::::X       X::::X  X:::::X        XXXXXX  X::::X     X:::::XX::::X    X:::::XXXXXXX XX:::::XX  X::::X X::::::X     XXXXXXX"
DATA "  X:::::XXXXXXXX::::XXX:::::::XXXXXXXX:::::XXX:::::XXXXXX::::::XX::::X    X:::::XX:::::XXXX::::::XX::::::XX:::::::XXXXXX:::::X"
DATA "   XX:::::::::::::::XX:::::::::::::::::::::XX:::::::::::::::::X X:::::XXXX::::::XX::::::::::::::X X::::::X X:::::::::::::::::X"
DATA "     XXX::::::XXX:::XX:::::::::::::::::::::XX::::::::::::::::X   X::::::::::XX:::XX:::::::::::XX  X::::::X  XX:::::::::::::::X"
DATA "        XXXXXX   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     XXXXXXXXXX  XXXX XXXXXXXXXXX    XXXXXXXX    XXXXXXXXXXXXXXXX"
ENDDATA

--- End code ---
additional OpenGL functions, paste to second file:

--- Code: (glbasic) ---INLINE
#ifdef CENTERLINE_CLPP
#define signed
#endif
#define OGL ::
typedef unsigned int    GLenum;
typedef unsigned char   GLboolean;
typedef unsigned int    GLbitfield;
typedef void            GLvoid;
typedef signed char     GLbyte;
typedef short           GLshort;
typedef int             GLint;
typedef unsigned char   GLubyte;
typedef unsigned short  GLushort;
typedef unsigned int    GLuint;
typedef int             GLsizei;
typedef float           GLfloat;
typedef float           GLclampf;
typedef double          GLdouble;
typedef double          GLclampd;
ENDINLINE
GLOBAL GL_ALPHA_TEST                                      = 0x0BC0
GLOBAL GL_ALPHA_TEST_REF                                  = 0x0BC2
GLOBAL GL_ALPHA_TEST_FUNC                                 = 0x0BC1
GLOBAL GL_BLEND                                           = 0x0BE2
GLOBAL GL_BLEND_SRC                                       = 0x0BE1
GLOBAL GL_BLEND_DST                                       = 0x0BE0
GLOBAL GL_ZERO                                            = 0x0
GLOBAL GL_ONE                                             = 0x1
GLOBAL GL_SRC_COLOR                                       = 0x0300
GLOBAL GL_ONE_MINUS_SRC_COLOR                             = 0x0301
GLOBAL GL_SRC_ALPHA                                       = 0x0302
GLOBAL GL_ONE_MINUS_SRC_ALPHA                             = 0x0303
GLOBAL GL_DST_ALPHA                                       = 0x0304
GLOBAL GL_ONE_MINUS_DST_ALPHA                             = 0x0305
GLOBAL GL_DST_COLOR                                       = 0x0306
GLOBAL GL_ONE_MINUS_DST_COLOR                             = 0x0307
GLOBAL GL_SRC_ALPHA_SATURATE                              = 0x0308
GLOBAL GL_CONSTANT_COLOR                                  = 0x8001
GLOBAL GL_ONE_MINUS_CONSTANT_COLOR                        = 0x8002
GLOBAL GL_CONSTANT_ALPHA                                  = 0x8003
GLOBAL GL_ONE_MINUS_CONSTANT_ALPHA                        = 0x8004



INLINE
    }
    extern "C" {
        #include <cstdio>
        void __stdcall glDisable( GLenum cap );
        void __stdcall glColorMask( GLboolean red , GLboolean green , GLboolean blue , GLboolean alpha );
        void __stdcall glAlphaFunc( GLenum func , GLclampf ref );
        void __stdcall glBlendFunc( GLenum sfactor , GLenum dfactor );
        void __stdcall glEnable( GLenum cap );
        const unsigned char* __stdcall glGetString( int name );
        }
    namespace __GLBASIC__ {
ENDINLINE


FUNCTION glColorMask: red, green, blue, alpha
INLINE
OGL glColorMask(red, green, blue, alpha);
ENDINLINE
ENDFUNCTION

FUNCTION glAlphaFunc: func, ref
INLINE
OGL glAlphaFunc(func, ref);
ENDINLINE
ENDFUNCTION

FUNCTION glBlendFunc: sfactor, dfactor
INLINE
OGL glBlendFunc(sfactor, dfactor);
ENDINLINE
ENDFUNCTION

FUNCTION glEnable: cap
INLINE
OGL glEnable(cap);
ENDINLINE
ENDFUNCTION

FUNCTION glDisable: cap
INLINE
OGL glDisable(cap);
ENDINLINE
ENDFUNCTION

FUNCTION glGetString$: id%
        INLINE
                return DGStr((const char*)glGetString(id));
        ENDINLINE
ENDFUNCTION

--- End code ---

Waiting for entries from others :P
btw. what about shaders? They can be used to make some crazy stuff, maybe separate them to another competition.

Qedo:
And instead another plasma / fire example.
Surely there is something better but I hope you like it and above all that it is useful.

ps It's not entirely my idea. In the program the references of inspiration

Hemlos:
Fantastic dreamerman, really nice!  :good:
And thank you for this, it might be useful for something i'm working on.

Hemlos:
Qedo i don't know how that works, but i want to figgure it out!
Looks really cool!

dreamerman:
@Qedo: Thank You, this will be usefully as it's looking really nice and there is no need to port/rewrite other fire effect if someone needs it :D

Background of fancy sprites with black-hole effect, and text filled with such nice fire as above. This would be great :> I hope that there will be more entries, there is still time.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version