banner


SDL intro


 

SDL es una muy buena librería gráfica de codigo abierto y multiplataforma, es tan simple que estaba siguiendo el tutorial y sin querer me hice un simple demo mientras escuchaba a Denisse de Belanova (Escena Final) ^_^...

 

source


//! =======================================================================
//! CODED BY: _ex_ ([email protected])
//! -----------------------------------------------------------------------
//! Based in the tutorial found in: 
//! http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/index
//!
//! programmed listening: BELANOVA - ESCENA FINAL (�DULCE BEAT 2005)
//! (gracias por la inspiracion Denisse!! ^_^)
//! -----------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "SDL.h"

// componentes de color globales
double gf_KRX = 0.0;
double gf_KGX = 0.0;
double gf_KBX = 0.0;
double gf_KRY = 0.0;
double gf_KGY = 0.0;
double gf_KBY = 0.0;

bool gb_Rotate = true;

// funcion para dibujar un pixel en pantalla
void DrawPixel (SDL_Surface *screen,
                int x,
                int y,
                Uint8 R,
                Uint8 G,
                Uint8 B)
{
    Uint32 color = SDL_MapRGB (screen->format, R, G, B);

    switch (screen->format->BytesPerPixel)
    {
    case 1: // Assuming 8-bpp
        {
            Uint8 *bufp;
            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
            *bufp = color;
        }
        break;

    case 2: // Probably 15-bpp or 16-bpp
        {
            Uint16 *bufp;
            bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
            *bufp = color;
        }
        break;

    case 3: // Slow 24-bpp mode, usually not used
        {
            Uint8 *bufp;
            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;

            if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
            {
                bufp[0] = color;
                bufp[1] = color >> 8;
                bufp[2] = color >> 16;
            }
            else
            {
                bufp[2] = color;
                bufp[1] = color >> 8;
                bufp[0] = color >> 16;
            }
        }
        break;

    case 4: // Probably 32-bpp
        {
            Uint32 *bufp;
            bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
            *bufp = color;
        }
        break;
    }
}


// Lock superficie
void Slock (SDL_Surface *screen)
{
    if (SDL_MUSTLOCK (screen))
    {
        if (SDL_LockSurface (screen) < 0)
        {
            return;
        }
    }
}


// Unlock superficie
void Sulock (SDL_Surface *screen)
{
    if (SDL_MUSTLOCK (screen))
    {
        SDL_UnlockSurface (screen);
    }
}

int SoftModule (int num, int mod)
{
    int ret = num % (2 * mod + 1);

    if (ret > mod)
    {
        ret = 2 * mod - ret + 1;
    }
    return ret;
}

Uint8 GetRedColor (int x, int y)
{
    return (SoftModule (int (gf_KRX * x + gf_KRY * y), 255));
}

Uint8 GetGreenColor (int x, int y)
{
    return (SoftModule (int (gf_KGX * x + gf_KGY * y), 255));
}

Uint8 GetBlueColor (int x, int y)
{
    return (SoftModule (int (gf_KBX * x + gf_KBY * y), 255));
}

void ModifyCoeffs ()
{
    static Uint32 k = 0;

    if (gb_Rotate)
    {
        k++;

        // I love math... ^_^
        gf_KRX = 1.25*sin (k / 15.0) + 1.25;
        gf_KGX = 1.25*sin (k / 20.0) + 1.25;
        gf_KBX = 1.25*sin (k / 25.0) + 1.25;
        gf_KRY = 1.25*cos (k / 25.0) + 1.25;
        gf_KGY = 1.25*cos (k / 15.0) + 1.25;
        gf_KBY = 1.25*cos (k / 10.0) + 1.25;
    }
}

void DrawScene (SDL_Surface *screen)
{
    // bloquear superficie si es necesario
    Slock (screen);

    ModifyCoeffs ();

    for (int x = 0; x < 640; x++)
    {
        for (int y = 0; y < 480; y++)
        {
            DrawPixel (screen, x, y, GetRedColor (x, y), GetGreenColor (x, y), GetBlueColor (x, y));
        }
    }

    // desbloquear superficie si es necesario
    Sulock (screen);

    // On hardware that supports double-buffering,
    // this function sets up a flip and returns.
    SDL_Flip (screen);
}


int main (int argc, char *argv[])
{
    // inicializar SDL
    if (SDL_Init (SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
    {
        printf ("Unable to init SDL: %s\n", SDL_GetError ());
        exit (1);
    }

    // indicar finalizar SDL al terminar
    atexit (SDL_Quit);

    SDL_Surface *screen;
    
    // crear superficie de pantalla
    screen = SDL_SetVideoMode (640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);

    if (screen == NULL)
    {
        printf ("Unable to set 640x480 video: %s\n", SDL_GetError());
        exit (1);
    }

    // dibujar algo...
    bool bolContinue = true;

    SDL_Event event;

    while (bolContinue)
    {
        // Polls for currently pending events
        while (SDL_PollEvent (&event))
        {
            if (event.type == SDL_QUIT)  {bolContinue = false;}

            if (event.type == SDL_KEYDOWN)
            {
                if (event.key.keysym.sym == SDLK_ESCAPE) {bolContinue = false;}

                if (event.key.keysym.sym == SDLK_SPACE) {gb_Rotate = (gb_Rotate == false);}
            }
        }

        DrawScene (screen);
    }
    return 0;
}

 

Claro que tienes que ver esto en movimiento, puedes descargar el ejecutable intro.zip o generar tu propio ejecutable intro_src.zip (VC++ 6)

 

 
 
 

 


Diseño y coordinación : Esaú Rodriguez Oscanoa - Lima - Per�

Esta página se ve mejor en Mozilla Firefox a 1024x768


Hosted by www.Geocities.ws

Hosted by www.Geocities.ws

1