PART I

(There will be allegro stuff, no networking still...)

As you may know zoidcom works on server-client based topology. So, we’ll need two projects, first will be server application and second client application. We will be writing only server app and then, easliy copy paste source to client, changing what need to be changed.

Start new project, name him as you wish. We will be using only one file for source code to make things easier.

Includes:

#include <zoidcom.h> //Zoidcom library include
#include <allegro.h> //Allegro library include
#include <conio.h>   //Windows library for getch() func

Our int main() function should be look like this:

int main() 
{
    start();
    do {
        DrawAll();
        GetInput();
        Process();
 
        
    } while(!exit_chat);     
    shutdown();
 
    
  return 0;   
}
END_OF_MAIN();

So, we’ll need to write all those functions. As you may guess we start with start() :)

A typical allegro start() function:

void start()
{
   allegro_init(); //Allegro needed function
   install_keyboard(); //Instaling keyboard
 
   //////////////// GRAPHICS INIT /////////////////////////////////
   set_color_depth(16); 
   if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) <= -1)
   {
      set_color_depth(15);
      if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) <= -1)
      {
         allegro_message("Error initializating graphics", NULL);
         exit(1);
      }
   }      
   //////////////// WINDOW PROCEDURES ////////////////////////////
   // Window title, write everything you want here
   set_window_title("Chatic v0.01 SERWER");
 
   // callback function for this litte [X] button in top right of
   // your window, we will write it soon, if you dont want it
   // just comment this line 
   set_close_button_callback(close_button_handler);
 
   //For working in background, very important.
   //If this line is missing, the application will just stop running when
   //another app is in the foreground and active
   set_display_switch_mode(SWITCH_BACKGROUND);
 
 
   //For double buffering
   buffer = create_bitmap(SCREEN_W,SCREEN_H);
 
   //Our chat will be composited with two windows:
   //talk_screen where messages are being displayed,
   //and write_screen where you type your message.
   //write_screen isnt really exist, we will write
   //directly on buffer
   // (-81) is size  of write_screen
   talk_screen = create_bitmap(SCREEN_W, SCREEN_H-81);
 
   //temporary bitmap, we will need it to make text move
   //smothly
   talk_screen_temp = create_bitmap(SCREEN_W, SCREEN_H-81);
 
   //clearing bitmaps
   clear_to_color(buffer, makecol(0, 0, 0));
   clear_to_color(talk_screen, makecol(128, 128, 128));
   clear_to_color(talk_screen_temp, makecol(128, 128, 128));
 
 
   //Configuration
   //I've maked simple config file to read your nickname
   //and other options, if you don't want to use it, comment
   //this but rememer to declare char* nickname = "nickname";
     push_config_state();
      set_config_file("config.ini");
      nickname = ustrdup(get_config_string("user", "name", "noname"));
     pop_config_state();
 
   //Messages on start
   textprintf_ex(talk_screen, font, 1, 1, makecol(255,255,255), -1, "Chatic version 0.01 SERVER started..."); 
   textprintf_ex(talk_screen, font, 1, 15, makecol(255,255,255), -1, "All ok..."); 
   
}

Now, we shold write DrawAll() function It’s pretty simple.

void DrawAll()
{
    //Lines for divide talk_screen from write_screen
    line(buffer, 0, SCREEN_H-79, SCREEN_W, SCREEN_H-79, makecol(192,192,192));
    line(buffer, 0, SCREEN_H-80, SCREEN_W, SCREEN_H-80, makecol(255,255,255));
    line(buffer, 0, SCREEN_H-81, SCREEN_W, SCREEN_H-81, makecol(192,192,192));
 
    //Your nickname should be always drawed to write_screen
    //I will explain why, later
    textprintf_ex(buffer, font, 1, SCREEN_H-76, makecol(255,255,255), -1, "%s>", nickname); 
 
    //Drawing talk_screen to buffer and buffer to screen
    blit(talk_screen, buffer, 0, 0, 0, 0, talk_screen->w, talk_screen->h);
    blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
     
}

Process() function you can leave blank for now and GetInput() is also very very simple...for now ofcourse

void GetInput()
{
     if(key[KEY_ESC]) exit_chat = true;
}

Shutdown, yea, easier than you may think...

void shutdown()
{
    destroy_bitmap(buffer);
    destroy_bitmap(talk_screen);
    destroy_bitmap(talk_screen_temp);
    allegro_exit();
}

Try to compile it If you get simple, two colored window it should be ok and you may proceed to part II

Complete source code:

#include <zoidcom.h> //Zoidcom library include
#include <allegro.h> //Allegro library include
#include <conio.h>   //Windows library for getch() func
 
BITMAP* buffer;
BITMAP* talk_screen;
BITMAP* talk_screen_temp;
 
bool exit_chat = false;
char* nickname;
 
 
 
void start()
{
   allegro_init(); //Allegro needed function
   install_keyboard(); //Instaling keyboard
 
   //////////////// GRAPHICS INIT /////////////////////////////////
   set_color_depth(16); 
   if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) <= -1)
   {
      set_color_depth(15);
      if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) <= -1)
      {
         allegro_message("Error initializating graphics", NULL);
         exit(1);
      }
   }      
   //////////////// WINDOW PROCEDURES ////////////////////////////
   // Window title, write everything you want here
   set_window_title("Chatic v0.01 SERWER");
 
   // callback function for this litte [X] button in top right of
   // your window, we will write it soon, if you dont want it
   // just comment this line 
   //set_close_button_callback(close_button_handler);
 
   //For working in background, very important.
   //You will get strange results if you dont put this line
   set_display_switch_mode(SWITCH_BACKGROUND);
 
 
   //For double buffering
   buffer = create_bitmap(SCREEN_W,SCREEN_H);
 
   //Our chat will be composited with two windows:
   //talk_screen where messages are being displayed,
   //and write_screen where you type your message.
   //write_screen isnt really exist, we will write
   //directly on buffer
   // (-81) is size of talk screen and also size of write_screen
   talk_screen = create_bitmap(SCREEN_W, SCREEN_H-81);
 
   //temporary bitmap, we will need it to make text move
   //smothly
   talk_screen_temp = create_bitmap(SCREEN_W, SCREEN_H-81);
 
   //clearing bitmaps
   clear_to_color(buffer, makecol(0, 0, 0));
   clear_to_color(talk_screen, makecol(128, 128, 128));
   clear_to_color(talk_screen_temp, makecol(128, 128, 128));
 
 
   //Configuration
   //I've maked simple config file to read your nickname
   //and other options, if you don't want to use it, comment
   //this but rememer to declare char* nickname = "nickname";
     push_config_state();
      set_config_file("config.ini");
      nickname = ustrdup(get_config_string("user", "name", "noname"));
     pop_config_state();
 
   //Messages on start
   textprintf_ex(talk_screen, font, 1, 1, makecol(255,255,255), -1, "Chatic version 0.01 SERVER started..."); 
   textprintf_ex(talk_screen, font, 1, 15, makecol(255,255,255), -1, "All ok..."); 
   
}
 
void DrawAll()
{
    //Lines for divide talk_screen from write_screen
    line(buffer, 0, SCREEN_H-79, SCREEN_W, SCREEN_H-79, makecol(192,192,192));
    line(buffer, 0, SCREEN_H-80, SCREEN_W, SCREEN_H-80, makecol(255,255,255));
    line(buffer, 0, SCREEN_H-81, SCREEN_W, SCREEN_H-81, makecol(192,192,192));
 
    //Your nickname should be always drawed to write_screen
    //I will explain why, later
    textprintf_ex(buffer, font, 1, SCREEN_H-76, makecol(255,255,255), -1, "%s>", nickname); 
 
    //Drawing talk_screen to buffer and buffer to screen
    blit(talk_screen, buffer, 0, 0, 0, 0, talk_screen->w, talk_screen->h);
    blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
     
}
 
void Process()
{
}
 
void GetInput()
{
     if(key[KEY_ESC]) exit_chat = true;
}
 
void shutdown()
{
    destroy_bitmap(buffer);
    destroy_bitmap(talk_screen);
    destroy_bitmap(talk_screen_temp);
    allegro_exit();
}
 
int main() 
{
    start();
    do {
        DrawAll();
        GetInput();
        Process();
 
        
    } while(!exit_chat);     
    shutdown();
 
    
  return 0;   
}
END_OF_MAIN();
 
allegrozoidcomchat1.txt · Last modified: 2006/10/25 20:16 by matic
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki