Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: anonimo12121 en 4 Julio 2011, 19:33 pm



Título: Necesito información para continuar con mi API
Publicado por: anonimo12121 en 4 Julio 2011, 19:33 pm
Buenas a todos, como bien explica el titulo del post quiero hacer una API para uso personal vamos así aumentar mi aprendizaje tambien, lo que quiero hacer básicamente es una interfaz que mediante cortos codigos escrito en mi programa cree un boton, pero no se como hacerlo para que realmente sea óptimo se comunique todo o algo así, no creo que me haya explicado bien xD.

Voy a copiaros un código de lo que he hecho hasta ahora por aburrimiento. ADVIERTO QUE MI CÓDIGO NO SON MUY BUENOS.
Código
  1. #include <SDL/SDL.h>
  2. #include <SDL/SDL_image.h>
  3. #include <SDL/SDL_ttf.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #define Rojo {255,0,0}
  7. using namespace std;
  8. class interfaz {
  9.    private:
  10.        SDL_Surface *SFuente,*scrd;
  11.        TTF_Font *TTFuente;
  12.        SDL_Color Color;
  13.        SDL_Rect Rect;
  14.        char *Fuente;
  15.        int interruptor;
  16.        int tipo; // 1 boton 2 cuadro de texto
  17.    public:
  18.        interfaz();
  19.        void on();
  20.        void off();
  21.        int MouseOver(SDL_Event evento);
  22.        int MouseClick(SDL_Event evento);
  23.        int Mouse(SDL_Event evento);
  24.        void CargarFuente(char *nombre);
  25.        void ColorBoton(SDL_Surface *screen);
  26.        void TextSelColor(int r,int g,int b);
  27.        void Boton(char *texto,int size,int posx,int posy);
  28.        void Texto(char *texto,int size,int posx,int posy);
  29.        void Act(SDL_Surface *screen);
  30. };
  31. interfaz::interfaz(){
  32.    interruptor=0;
  33.    Color.r=0;
  34.    Color.g=0;
  35.    Color.b=0;
  36. };
  37. void interfaz::on(){
  38.    interruptor=1;
  39. };
  40. void interfaz::off(){
  41.    interruptor=0;
  42. };
  43. int interfaz::MouseOver(SDL_Event evento){
  44.    if((evento.motion.x >= Rect.x-2 && evento.motion.x <= Rect.x+Rect.w+1)
  45.    && (evento.motion.y >= Rect.y-2 && evento.motion.y <= Rect.y+Rect.h+3)){
  46.        on();
  47.        return 1;
  48.    }
  49.    else {
  50.        off();
  51.        return 0;
  52.    }
  53. };
  54. int interfaz::MouseClick(SDL_Event evento){
  55.    if(MouseOver(evento)==1 && evento.button.button == SDL_BUTTON_LEFT){
  56.        return 1;
  57.    }
  58.    else return 0;
  59. };
  60. int interfaz::Mouse(SDL_Event evento){
  61.    if(MouseClick(evento) ==1){
  62.        return 1;
  63.    }
  64.  
  65.    else return 0;
  66. };
  67. void interfaz::CargarFuente(char *nombre){
  68.    Fuente=nombre;
  69. };
  70. void interfaz::ColorBoton(SDL_Surface *screen){
  71.    int r,g,b;
  72.    if(interruptor==1){r=243;g=148;b=0;}
  73.    else {r=0;g=0;b=0;}
  74.    SDL_Rect x;
  75.    x.x=Rect.x-1;
  76.    x.y=Rect.y-1;
  77.    x.w=Rect.w+2;
  78.    x.h=Rect.h+2-(Rect.h/2);
  79.    SDL_FillRect(screen,&x,SDL_MapRGB(screen->format,223,235,253));
  80.    x.y=x.y+x.h;
  81.    SDL_FillRect(screen,&x,SDL_MapRGB(screen->format,212,230,255));
  82.    x.x=x.x-1;
  83.    x.y=Rect.y-2;
  84.    x.w=x.w+2;
  85.    x.h=1;
  86.    SDL_FillRect(screen,&x,SDL_MapRGB(screen->format,r,g,b));
  87.    x.y=Rect.y+Rect.h+3;
  88.    SDL_FillRect(screen,&x,SDL_MapRGB(screen->format,r,g,b));
  89.    x.x=Rect.x-2;
  90.    x.y=Rect.y-1;
  91.    x.w=1;
  92.    x.h=Rect.h+4;
  93.    SDL_FillRect(screen,&x,SDL_MapRGB(screen->format,r,g,b));
  94.    x.x=Rect.x+Rect.w+1;
  95.    SDL_FillRect(screen,&x,SDL_MapRGB(screen->format,r,g,b));
  96. };
  97.  
  98. void interfaz::TextSelColor(int r,int g,int b){
  99.    Color.r=r;
  100.    Color.g=g;
  101.    Color.b=b;
  102. };
  103. void interfaz::Boton(char *texto,int size,int posx,int posy){
  104.    tipo=1;
  105.    TTFuente=TTF_OpenFont(Fuente, size);
  106.    SFuente= TTF_RenderText_Blended(TTFuente,texto,Color);
  107.    Rect.x=posx;
  108.    Rect.y=posy;
  109.    Rect.w=SFuente->w;
  110.    Rect.h=SFuente->h;
  111. };
  112. void interfaz::Texto(char *texto,int size,int posx,int posy){
  113.    tipo=2;
  114.    TTFuente=TTF_OpenFont(Fuente, size);
  115.    SFuente= TTF_RenderText_Blended(TTFuente,texto,Color);
  116.    Rect.x=posx;
  117.    Rect.y=posy;
  118.    Rect.w=SFuente->w;
  119.    Rect.h=SFuente->h;
  120. };
  121. void interfaz::Act(SDL_Surface *screen){
  122.    if(tipo==1)ColorBoton(screen);
  123.    SDL_BlitSurface(SFuente,0,screen,&Rect);
  124. };
  125. //lineas rojas///////////
  126. SDL_Rect h={0,0,640,1};
  127. SDL_Rect v={0,0,1,480};
  128. /////////////////////////
  129. //color de fondo
  130. int br=0,bg=220,bb=0;
  131. /////////////////////////
  132. int main(int argc,char *argv[]) {
  133.    //Variables
  134.    SDL_Surface *screen;
  135.    SDL_Event evento;//Estructura evento
  136.    int start;
  137.    SDL_Init(SDL_INIT_VIDEO);
  138.    screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  139.    TTF_Init();
  140.    //FUENTE/////////////////////////////////////////////
  141.    interfaz i;
  142.    interfaz i2;
  143.    interfaz i3;
  144.    interfaz i4;
  145.    //i.ini(screen);
  146.    i.TextSelColor(0,0,0);
  147.    i.CargarFuente("LCD.ttf");
  148.    i2.CargarFuente("LCD.ttf");
  149.    i3.CargarFuente("LCD.ttf");
  150.    i4.CargarFuente("arial.ttf");
  151.    i.Boton("Azul",24,50,50);
  152.    i2.Boton("Verde",24,250,50);
  153.    i3.Boton("Cerrar",12,400,400);
  154.    i4.Texto("Texto",12,300,400);
  155.  
  156.    /////////////////////////////////////////////////////
  157.    while(1){
  158.  
  159.        start=SDL_GetTicks();
  160.        if(start%25==0){
  161.            SDL_PollEvent(&evento);//devuelve 0 si no hay eventos
  162.            v.x=evento.motion.x;
  163.            h.y=evento.motion.y;
  164.            if(evento.key.state==SDL_PRESSED){
  165.                    if(evento.key.keysym.sym == SDLK_ESCAPE){
  166.                        return 0;
  167.                    }
  168.            }
  169.            if(i.Mouse(evento) ==1){br=0;bg=0;bb=255;}
  170.            if(i2.Mouse(evento) ==1){br=0;bg=255;bb=0;}
  171.            if(i3.Mouse(evento) ==1)return 0;
  172.            SDL_FillRect(screen,0,SDL_MapRGB(screen->format,br,bg,bb));
  173.            i.Act(screen);
  174.            i2.Act(screen);
  175.            i3.Act(screen);
  176.            i4.Act(screen);
  177.            SDL_FillRect(screen,&h,SDL_MapRGB(screen->format,255,0,0));
  178.            SDL_FillRect(screen,&v,SDL_MapRGB(screen->format,255,0,0));
  179.            SDL_Flip(screen);
  180.        }
  181.    }
  182.    return 0;
  183. }

Esto es lo que llevo hasta el momento paso a explicar directamente el main() y ya me decis vuestra opinión.

Las lineas siguientes son para crear objetos.
Código
  1. interfaz i;
  2.    interfaz i2;
  3.    interfaz i3;
  4.    interfaz i4;
  5.  
Esta linea es para darle color al texto
Código
  1. i.TextSelColor(0,0,0);
Estas lineas son para cargar la fuente de texto de cada objeto
Código
  1. i.CargarFuente("LCD.ttf");
  2.    i2.CargarFuente("LCD.ttf");
  3.    i3.CargarFuente("LCD.ttf");
  4.    i4.CargarFuente("arial.ttf");
Estas lineas son para crear un boton y un texto.
Código
  1. i.Boton("Azul",24,50,50);
  2.    i2.Boton("Verde",24,250,50);
  3.    i3.Boton("Cerrar",12,400,400);
  4.    i4.Texto("Texto",12,300,400);
y las lineas similares a
Código
  1. i.Mouse(evento)
Son para devolver información sobre el estado del raton
A y las estas son para visualizar el objeto
Código
  1. i.Act(screen);


Paso a decir un poco sobre algunas funciones de la clase.
esta funcion devuelve un valor si esta el raton encima del objeto.
Código
  1. MouseOver
Y esta funcion devuelve un valor si el raton a pulsado click izquierdo encima del objeto.
Código
  1. MouseClick



Me gustaría que me dierais vuesta opinión y tantos consejos como pudieran ser para aprender para próximas cosas.
Saludos