Raydium 3D Game Engine

Official forum for everything about Raydium, ManiaDrive, MeMak, ...
It is currently Tue Apr 16, 2024 6:49 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Aug 28, 2008 9:53 am 
Offline
User avatar

Joined: Thu Sep 29, 2005 2:59 pm
Posts: 828
From the game I'm doing I have done this editor for our street system.
The street system is a paths-system in fact, the simplest version of a Pathfinder.

Here the code:

STREETS-EDITOR.C
Code:
/*
    VersusRX (Supertitititran2 no-oficial)
    License: LGPL + BSD (see LICENSE.txt file)
    Vicente Carro Fernandez, 1 abril 2008. vicentecarro@andaluciajunta.es
*/

#include "streets-editor.h"

#define MAX_POINTS 512
#define MAX_LINES 1024

char map[RAYDIUM_MAX_NAME_LEN];
float camera[3] = {0.01, 0.01, 2};
float pos[3] = {0.01, 0.01, 0};
float ang = 35;
float x, y;
float zoom = 2.0f;
float dt = 0;
int current = 0;
int previous = 0;
int mode = 0; //0=free, 1=points; 2=lines
//points
float point[MAX_POINTS][2];
int point_used[MAX_POINTS];
int firstpoint = 0;
float prevpoint[2];
int sel_point = 0;
int sel_point2 = 0;
//lines
//float line[MAX_LINES][4];
int line_used[MAX_LINES];
int autoline = 1;
int sel_line = 0;
int line_point[MAX_LINES][2];

void lines_init ( void )
{
    int a;

    for ( a = 0;a < MAX_LINES;a++ )
        {
            //line[a][0]=0;
            //line[a][1]=0;
            //line[a][2]=0;
            //line[a][3]=0;
            line_used[a] = 0;
            line_point[a][0] = 0;
            line_point[a][1] = 0;
        }
}

int line_add ( int p1, int p2 )
{
    int a;

    if ( p1 != p2 )
        for ( a = 0;a < MAX_LINES;a++ )
            {
                if ( line_used[a] == 0 )
                    {
                        //line[a][0]=point[p1][0];
                        //line[a][1]=point[p1][1];
                        //line[a][2]=point[p2][0];
                        //line[a][3]=point[p2][1];
                        line_point[a][0] = p1;
                        line_point[a][1] = p2;
                        line_used[a] = 1;
                        sel_line = a;
                        raydium_log ( "Added line %d from point %d to point %d", a, p1, p2 );
                        return a;
                    }
            }

    return -1;
}

int line_remove ( int a )
{
    if ( line_used[a] )
        {
            //line[a][0]=0;
            //line[a][1]=0;
            //line[a][2]=0;
            //line[a][3]=0;
            line_used[a] = 0;
            line_point[a][0] = 0;
            line_point[a][1] = 0;
            raydium_log ( "Deleted line %d", a );
            return 1;
        }

    return 0;
}

int line_next ( int cur )
{
    int a;

    for ( a = cur + 1;a < MAX_LINES;a++ )
        {
            if ( line_used[a] )
                {
                    return a;
                }
        }

    return line_first();
}

int line_prev ( int cur )
{
    int a;

    for ( a = cur - 1;a >= 0;a-- )
        {
            if ( line_used[a] )
                {
                    return a;
                }
        }

    return line_last();
}

int line_first ( void )
{
    int a;

    for ( a = 0;a < MAX_LINES;a++ )
        {
            if ( line_used[a] )
                {
                    return a;
                }
        }

    return -1;
}

int line_last ( void )
{
    int a;

    for ( a = MAX_LINES - 1;a >= 0;a-- )
        {
            if ( line_used[a] )
                {
                    return a;
                }
        }

    return -1;
}

int point_next ( int cur )
{
    int a;

    for ( a = cur + 1;a < MAX_POINTS;a++ )
        {
            if ( point_used[a] )
                {
                    return a;
                }
        }

    return point_first();
}

int point_prev ( int cur )
{
    int a;

    for ( a = cur - 1;a >= 0;a-- )
        {
            if ( point_used[a] )
                {
                    return a;
                }
        }

    return point_last();
}

int point_first ( void )
{
    int a;

    for ( a = 0;a < MAX_POINTS;a++ )
        {
            if ( point_used[a] )
                {
                    return a;
                }
        }

    return -1;
}

int point_last ( void )
{
    int a;

    for ( a = MAX_POINTS - 1;a >= 0;a-- )
        {
            if ( point_used[a] )
                {
                    return a;
                }
        }

    return -1;
}

int point_add ( float x, float y )
{
    int a;

    for ( a = 0;a < MAX_POINTS;a++ )
        {
            if ( point_used[a] == 0 )
                {
                    point[a][0] = x;
                    point[a][1] = y;
                    point_used[a] = 1;
                    raydium_log ( "Added Point %d at %.2f, %.2f", a, x, y );
                    return a;
                }
        }

    return -1;
}

int point_remove ( int a )
{
    if ( point_used[a] )
        {
            point[a][0] = 0;
            point[a][1] = 0;
            point_used[a] = 0;
            raydium_log ( "Deleted point %d", a );
            //all the lines related have to be erased
            int b;

            for ( b = 0;b < MAX_LINES;b++ )
                {
                    if ( line_point[b][0] == a || line_point[b][1] == a )
                        line_remove ( b );
                }

            return 1;
        }

    return 0;
}

void draw_lines ( void )
{
    //dt+=raydium_frame_time*5;
    //if(dt>1)dt=0;
    raydium_camera_replace();
    int a;
    glLineWidth ( 2.3 );

    for ( a = 0;a < MAX_LINES;a++ )
        {
            if ( line_used[a] )
                {
                    raydium_rendering_internal_prepare_texture_render ( 0 );

                    if ( sel_line == a )     glColor4f ( 1, 1, 0, 1 );
                    else
                        glColor4f ( 1, 0, 0, 1 );

                    if ( sel_line == a )     glLineWidth ( 4.5 );
                    else                glLineWidth ( 2.3 );

                    glBegin ( GL_LINE_STRIP );

                    glNormal3f ( 0.0f, 0.0f, -1.0f );

                    glVertex3f ( point[line_point[a][0]][0], point[line_point[a][0]][1], 0.001 );

                    if ( sel_line == a )     glColor4f ( 0, 1, 1, 1 );

                    glVertex3f ( point[line_point[a][1]][0], point[line_point[a][1]][1], 0.001 );

                    //glVertex3f(line[a][2],line[a][3],0.0019);
                    glEnd();

                }
        }

    raydium_camera_replace();
}

void draw_points ( void )
{
    dt += raydium_frame_time * 5;

    if ( dt > 1 ) dt = 0;

    raydium_camera_replace();

    int a;

    glLineWidth ( 2.3 );

    for ( a = 0;a < MAX_POINTS;a++ )
        {
            if ( point_used[a] )
                {
                    raydium_rendering_internal_prepare_texture_render ( 0 );

                    if ( sel_point == a )
                        {
                            glColor4f ( 1, 1, 0, 1 );
                            glBegin ( GL_LINE_STRIP );
                            glNormal3f ( 0.0f, 0.0f, -1.0f );
                            glVertex3f ( 0, 0, 0.01 );
                            glEnd();
                            glVertex3f ( 0, 0, 0.01 );
                            glRectf ( point[a][0] - 0.03,
                                      point[a][1] - 0.03,
                                      point[a][0] + 0.03,
                                      point[a][1] + 0.03 );
                        }

                    if ( sel_point2 == a )
                        {
                            glColor4f ( 0, 1, 1, 1 );
                            glBegin ( GL_LINE_STRIP );
                            glNormal3f ( 0.0f, 0.0f, -1.0f );
                            glVertex3f ( 0, 0, 0.01 );
                            glEnd();
                            glVertex3f ( 0, 0, 0.01 );
                            glRectf ( point[a][0] - 0.03,
                                      point[a][1] - 0.03,
                                      point[a][0] + 0.03,
                                      point[a][1] + 0.03 );
                        }

                    {
                        glColor4f ( 1, 0, 0, dt );
                        glBegin ( GL_LINE_STRIP );
                        glNormal3f ( 0.0f, 0.0f, -1.0f );
                        glVertex3f ( 0, 0, 0.01 );

                        glEnd();
                        glVertex3f ( 0, 0, 0.01 );
                        glRectf ( point[a][0] - 0.015,
                                  point[a][1] - 0.015,
                                  point[a][0] + 0.015,
                                  point[a][1] + 0.015 );
                    }
                }
        }

    raydium_camera_replace();
}

void points_init ( void )
{
    int a;

    for ( a = 0;a < MAX_POINTS;a++ )
        {
            point_used[a] = 0;
            point[a][0] = 0;
            point[a][1] = 0;
        }
}

void info()
{
    char tmp[RAYDIUM_MAX_NAME_LEN];

    raydium_osd_draw_name ( "back.tga", 0, 0, 100, 24 );
    raydium_osd_draw_name ( "back.tga", 0, 100, 100, 85 );
    raydium_osd_color_change ( 1, 0, 0 );

    switch ( mode )
        {

        case 0:
            strcpy ( tmp, "Free drawing mode" );
            break;

        case 1:
            strcpy ( tmp, "Point mode" );
            break;

        case 2:
            strcpy ( tmp, "Line mode" );
            break;

        default:
            strcpy ( tmp, "" );
        }

    raydium_osd_printf ( 3, 3, 18, 0.5, "font2.tga", "Mode: %s (F1 to change mode)", tmp );

    switch ( autoline )
        {

        case 0:
            strcpy ( tmp, "OFF" );
            break;

        case 1:
            strcpy ( tmp, "ON" );
            break;

        default:
            strcpy ( tmp, "" );
        }

    raydium_osd_printf ( 3, 6, 18, 0.5, "font2.tga", "Autocreation of lines: %s (F5 to switch)", tmp );

    raydium_osd_printf ( 3, 9, 18, 0.5, "font2.tga", "F9 - Save this street file." );
    raydium_osd_printf ( 3, 12, 18, 0.5, "font2.tga", "F10 - Load the same street file." );
    raydium_osd_printf ( 3, 15, 18, 0.5, "font2.tga", "F11 - Reset scene." );
    raydium_osd_printf ( 3, 18, 18, 0.5, "font2.tga", "F12 - Exit the application." );
    raydium_osd_printf ( 3, 21, 18, 0.5, "font2.tga", "+/- to change zoom" );

    if ( mode == 0 )
        {
            raydium_osd_printf ( 3, 96, 18, 0.5, "font2.tga", "Use cursors to move the pointer." );
            raydium_osd_printf ( 3, 93, 18, 0.5, "font2.tga", "Space to add a point." );
        }

    if ( mode == 1 )
        {
            raydium_osd_printf ( 3, 96, 18, 0.5, "font2.tga", "Use left/right cursors to change the A point" );
            raydium_osd_printf ( 3, 93, 18, 0.5, "font2.tga", "Use up/down cursors to change the B point" );
            raydium_osd_printf ( 3, 90, 18, 0.5, "font2.tga", "Use L key to draw a line" );
            raydium_osd_printf ( 3, 87, 18, 0.5, "font2.tga", "Use X key to delete A point(and all related lines)" );
        }

    if ( mode == 2 )
        {
            raydium_osd_printf ( 3, 96, 18, 0.5, "font2.tga", "Use left/right cursors to change the line" );
            raydium_osd_printf ( 3, 93, 18, 0.5, "font2.tga", "Use X key to delete the line" );
        }

    raydium_osd_color_change ( 1, 1, 1 );
}


void display ( void )
{
    //START  //////////////////////////////////////////////////////////
    raydium_clear_frame();

    //PROCESSES  //////////////////////////////////////////////////////

    //INPUT  //////////////////////////////////////////////////////////
    //ESC to EXIT

    if ( raydium_key_last == 1027 )
        exit ( 0 );

    //ZOOM
    if ( raydium_key_last == 1000 + '-' )
        {
            //zoom-=0.5*raydium_frame_time;
            zoom += 0.1 * zoom;
        }

    if ( raydium_key_last == 1000 + '+' )
        {
            //zoom+=0.5*raydium_frame_time;
            zoom -= 0.1 * zoom;
        }

    //MODE 0

    if ( mode == 0 )
        {
            if ( raydium_key[GLUT_KEY_LEFT] )
                {
                    x += zoom * 0.2 * raydium_frame_time;
                }

            if ( raydium_key[GLUT_KEY_RIGHT] )
                {
                    x -= zoom * 0.2 * raydium_frame_time;
                }

            if ( raydium_key[GLUT_KEY_DOWN] )
                {
                    y += zoom * 0.2 * raydium_frame_time;
                }

            if ( raydium_key[GLUT_KEY_UP] )
                {
                    y -= zoom * 0.2 * raydium_frame_time;
                }











            if ( raydium_key_last == 1000 + ' ' )
                {
                    current = point_add ( x, y );

                    if ( autoline )
                        {
                            if ( firstpoint )
                                line_add ( current, previous );

                            prevpoint[0] = x;

                            prevpoint[1] = y;

                            previous = current;
                        }

                    if ( !firstpoint )
                        {
                            firstpoint = 1;
                            previous = current;
                        }
                }
        }

    if ( mode == 1 )
        {
            if ( raydium_key_last == GLUT_KEY_RIGHT )
                {
                    sel_point = point_next ( sel_point );
                }

            if ( raydium_key_last == GLUT_KEY_LEFT )
                {
                    sel_point = point_prev ( sel_point );
                }

            if ( raydium_key_last == GLUT_KEY_UP )
                {
                    sel_point2 = point_next ( sel_point2 );
                }

            if ( raydium_key_last == GLUT_KEY_DOWN )
                {
                    sel_point2 = point_prev ( sel_point2 );
                }

            if ( raydium_key_last == 1000 + 'l' )
                {
                    sel_line = line_add ( sel_point, sel_point2 );
                }

            if ( raydium_key_last == 1000 + 'x' )
                {
                    point_remove ( sel_point );
                }
        }

    if ( mode == 2 )
        {
            if ( raydium_key_last == GLUT_KEY_RIGHT )
                {
                    sel_line = line_next ( sel_line );
                }

            if ( raydium_key_last == GLUT_KEY_LEFT )
                {
                    sel_line = line_prev ( sel_line );
                }

            if ( raydium_key_last == 1000 + 'x' )
                {
                    line_remove ( sel_line );
                }
        }

    if ( raydium_key_last == 1 )
        {
            mode++;

            if ( mode >= 3 ) mode = 0;
        }

    if ( raydium_key_last == 5 )
        {
            //switch autoline
            autoline = autoline ? 0 : 1;
        }

    if ( raydium_key_last == 9 )
        {
            export_streets();
        }

    if ( raydium_key_last == 10 )
        {
            import_streets ( "mapa-giralda.streets" );
        }

    if ( raydium_key_last == 11 )
        {
            points_init();
            lines_init();
        }

    if ( raydium_key_last == 12 )
        {
            exit ( 0 );
        }


    //RENDER PIPELINE  ////////////////////////////////////////////////

    raydium_camera_smooth ( x, y + 0.001, zoom, y, 0, x, ang, 0.0f, raydium_frame_time*3 );

    //raydium_camera_look_at (x,y+0.001,zoom,y,0,x);
    draw_map();

    draw_lines();

    draw_points();

    draw_pointer();

    raydium_ode_draw_all ( 0 );

    info();

    //FINISH  /////////////////////////////////////////////////////////
    raydium_rendering_finish();
}

void draw_pointer ( void )
{
    raydium_camera_replace();
    raydium_rendering_internal_prepare_texture_render ( 0 );
    glColor3f ( 1, 0, 0 );
    glBegin ( GL_LINE_STRIP );
    glNormal3f ( 0.0f, 0.0f, -1.0f );
    glVertex3f ( x - 0.01, y + 0.01, 0.02 );
    glVertex3f ( x + 0.01, y + 0.01, 0.02 );
    glVertex3f ( x + 0.01, y - 0.01, 0.02 );
    glVertex3f ( x - 0.01, y - 0.01, 0.02 );
    glVertex3f ( x - 0.01, y + 0.01, 0.02 );
    glEnd();
    raydium_camera_replace();
}

void draw_map()
{
    raydium_camera_replace();
    raydium_rendering_internal_prepare_texture_render ( raydium_texture_find_by_name ( map ) );
    glColor4f ( 1, 1, 1, 1 );

    glBegin ( GL_QUADS );
    glNormal3f ( 0.0f, 0.0f, 1.0f );
    glTexCoord2f ( 0.0f, 1.0f );
    glVertex3f ( -1, 1, -0.0001 );

    glTexCoord2f ( 1.0f, 1.0f );
    glVertex3f ( 1, 1, -0.0001 );

    glTexCoord2f ( 1.0f, 0.0f );
    glVertex3f ( 1, -1, -0.0001 );

    glTexCoord2f ( 0.0f, 0.0f );
    glVertex3f ( -1, -1, -0.0001 );
    glEnd();
    raydium_camera_replace();
}

int export_streets()
{
    FILE *file;
    char tmp[RAYDIUM_MAX_NAME_LEN];
    strncpy ( tmp, map, strlen ( map ) - 4 );
    strcat ( tmp, ".streets" );

    if ( file = raydium_file_fopen ( tmp, "w" ) )
        {
            //the texture map
            fprintf ( file, "//Streets file for %s texture map\n", map );
            fprintf ( file, "//Please check that the texture map is square\n" );
            fprintf ( file, "title=\"%s\"\n\n", tmp );

            fprintf ( file, "map=\"%s\"\n\n", map );
            fprintf ( file, "//list of crosses of streets\n" );
            fprintf ( file, "//cross={ident, x coord, y coord}\n" );
            int a;

            for ( a = 0;a < MAX_POINTS;a++ )
                {
                    if ( point_used[a] )
                        fprintf ( file, "cross={%d,%f,%f}\n", a, point[a][0], point[a][1] );
                }

            fprintf ( file, "\n" );

            fprintf ( file, "//list of streets\n" );
            fprintf ( file, "//street={ident, 1stcross, 2ndcross}\n" );

            for ( a = 0;a < MAX_LINES;a++ )
                {
                    if ( line_used[a] )
                        fprintf ( file, "street={%d,%d,%d}\n", a, line_point[a][0], line_point[a][1] );
                }

            fprintf ( file, "//EOF" );

            fclose ( file );
            return 1;
        }

    return 0;
}

int import_streets ( char *filename )
{
    FILE *file;
    int ret;
    char var[RAYDIUM_MAX_NAME_LEN];
    char val_s[RAYDIUM_MAX_NAME_LEN];
    GLfloat val_f[5];
    int size;
    points_init();
    lines_init();

    if ( file = raydium_file_fopen ( filename, "r" ) )
        {
            while ( ( ret = raydium_parser_read ( var, val_s, val_f, &size, file ) ) != RAYDIUM_PARSER_TYPE_EOF )
                {
                    if ( strcmp ( var, "map" ) == 0 && ret == RAYDIUM_PARSER_TYPE_STRING )
                        {
                            strcpy ( map, val_s );
                            raydium_log ( "map: %s", val_s );
                        }

                    if ( strcmp ( var, "title" ) == 0 && ret == RAYDIUM_PARSER_TYPE_STRING )
                        {
                            strcpy ( map, val_s );
                            raydium_log ( "title: %s", val_s );
                        }

                    if ( strcmp ( var, "cross" ) == 0 && ret == RAYDIUM_PARSER_TYPE_FLOAT && size == 3 )
                        {
                            point[ ( int ) val_f[0]][0] = val_f[1];
                            point[ ( int ) val_f[0]][1] = val_f[2];
                            point_used[ ( int ) val_f[0]] = 1;
                            raydium_log ( "cross:%d,%f,%f", ( int ) val_f[0], val_f[1], val_f[2] );
                        }

                    if ( strcmp ( var, "street" ) == 0 && ret == RAYDIUM_PARSER_TYPE_FLOAT && size == 3 )
                        {
                            line_point[ ( int ) val_f[0]][0] = val_f[1];
                            line_point[ ( int ) val_f[0]][1] = val_f[2];
                            line_used[ ( int ) val_f[0]] = 1;
                            raydium_log ( "line:%d,%f,%f", ( int ) val_f[0], val_f[1], val_f[2] );
                        }

                }

            fclose ( file );

            return 1;
        }

    return 0;
}

int main ( int argc, char **argv )
{
    raydium_init_args ( argc, argv );
    //loading the configuration file
    raydium_init_load ( "streets-editor.config" );

    if ( !raydium_init_cli_option ( "map", map ) )
        exit ( 0 );

    //Setting up light
    float lightpos[3] = {12, 1, 100};

    raydium_light_move ( 0, lightpos );

    points_init();

    lines_init();

    raydium_texture_filter_change ( "bilinear" );

    raydium_texture_compression ( TRUE );

    raydium_callback ( &display );

    return ( 0 );
}

// EOF




STREETS_EDITOR.H
Code:
#ifndef STREETS_EDITOR_H
#define STREETS_EDITOR_H
#include "raydium/index.c"
#endif





STREETS-EDITOR.CONFIG
Code:
//Automatidally generated default config file. You can custom it.
//window width (default:  800)
width=1024
//window height (default:  600)
height=768
//window style: window or fullscreen (default: window )
windowtype="window"
//title of the window,application (default:  Raydium application 0.1)
title="Street Editor"
//texture filter: none, bilinear, trilinear, aniso (default:  trilinear)
filter="aniso"
//view angle, ie fov (default:  60)
fov=60
//distance to the nearets object(plane) draw   (default:  0.001)
near=0.01
//distance to the further   object(plane) draw (default:  2500)
far=100
//fog: on/enable or off/disable (default:  off)
fog="off"
//lighting: on/enable or off/disable (default: on )
lighting="on"
//light 0 parameters (default:  0,50,150,200,1000000,1,0.9,0.7)
light0=0,50,150,200,1000000,1,0.9,0.7
//background color (default:  1,0.9,0.7,1)
background=1,0.9,0.7,1
//Fake HDR effect(default:off)
hdr="off"
//data can be foldered into "data" folders and its subfolders:
//"foldered" or anything else. (default:"foldered")
paths="foldered"
//Sky type: "box" or "dynamic" or "none"
sky="none"


You also need to donwload these textures:


You can launch editor with:

./odyncomp.sh streets-editor.c (to create the test executable, even if it will crash)
export LD_LIBRARY_PATH=.
./test --map mapa-giralda.tga (or your own map)

Usage:
At start you should disable auto-creation of lines pressing F5.
Then you can create your crosses(point that will be used to define lines)
When you are ready, press F1 to change the mode.
Now you can change a pair of crosses using the directional buttons.
With a pair of crosses selected you can create an street pressing L button
Also you can delete the yellow cross pressing X
If you change again of mode you can choose lines and also delete it with X.
If you change the mode again you will go back to the first mode(Creation of crosses).

Now I'm working to allow moving throught those streets(lines) and crosses(points), but is quite hardest than it looked.
Someone wants to help?

Email and chatting:
vicentecarro at gmail dot com
or
vicentecarro at hotmail dot com

I've noticed a few bugs when launching this app from raydium folder but I have no time to fix'em. I hope to have a break soon(when i finish the game)
I'll back! ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 7:53 pm 
Offline
User avatar

Joined: Sun Mar 16, 2003 2:53 am
Posts: 2591
Location: gnniiiii (Scrat)
Hi,

Just tried to test this, but I'm currently unable to get a correct display :(

Looks like this:
http://ftp.cqfd-corp.org/raycap2008-09-02-193938-00.jpg

Any idea ?

PS: With a Raydium compliant style :) , this app may became a contrib, no ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 10:07 pm 
Offline
User avatar

Joined: Thu Sep 29, 2005 2:59 pm
Posts: 828
I hope to fix that this weekend.

And yes, maybe it could be a contrib ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group