From 37061920bc263e9d5350aa2d83a7e7785fedc1df Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 12 Dec 2004 14:15:33 +0000 Subject: Added Guichan and converted login dialog. Also resolved some naming conflicts. --- src/gui/gui.cpp | 84 ++++++++++++++++++----- src/gui/gui.h | 4 ++ src/gui/login.cpp | 198 ++++++++++++++++++++++++++++++++++++------------------ src/gui/login.h | 9 ++- src/gui/setup.cpp | 4 +- src/map.cpp | 45 +++++++------ src/map.h | 4 +- 7 files changed, 239 insertions(+), 109 deletions(-) diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 1ef3dea4..b9dcf923 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -19,11 +19,12 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "gui.h" -#include "../log.h" -#include "allegro/internal/aintern.h" +#include +#include #include #include +#include "gui.h" +#include "../log.h" #include "../sound/sound.h" #ifndef WIN32 @@ -55,24 +56,65 @@ extern TmwSound sound; int (*gui__external_slider_callback)(void *, int); int reroute_slider_proc(void *dp3, int d2); +// Guichan Allegro stuff +gcn::AllegroInput* input; // Input driver +gcn::AllegroGraphics* graphics; // Graphics driver +gcn::AllegroImageLoader* imageLoader; // For loading images + +// Guichan stuff +gcn::Gui* gui; // A Gui object - binds it all together +gcn::Container* guitop; // The top container +gcn::ImageFont* guiFont; // A font + + /** Initialize gui system */ void init_gui(BITMAP *bitmap, const char *skin) { - gui_bitmap = bitmap; - gui_load_skin(skin); - //alfont_init(); - gui_font = alfont_load_font("./data/Skin/arial.ttf"); - alfont_set_font_size(gui_font, 14); - drag = false; - show_mouse(NULL); + imageLoader = new gcn::AllegroImageLoader(); + gcn::Image::setImageLoader(imageLoader); + + graphics = new gcn::AllegroGraphics(); + graphics->setTarget(bitmap); + + input = new gcn::AllegroInput(); + + guitop = new gcn::Container(); + guitop->setDimension(gcn::Rectangle(0, 0, SCREEN_W, SCREEN_H)); + guitop->setOpaque(false); + + gui = new gcn::Gui(); + gui->setGraphics(graphics); + gui->setInput(input); + gui->setTop(guitop); + guiFont = new gcn::ImageFont("./data/graphic/fixedfont.bmp", + " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); + gcn::Widget::setGlobalFont(guiFont); + + + gui_bitmap = bitmap; + gui_load_skin(skin); + //alfont_init(); + gui_font = alfont_load_font("./data/Skin/arial.ttf"); + alfont_set_font_size(gui_font, 14); + drag = false; + show_mouse(NULL); } int gui_update(DIALOG_PLAYER *player) { - dialog_message(player->dialog, MSG_DRAW, 0, 0); - int ret = update_dialog(player); - draw_sprite(gui_bitmap, mouse_sprite, mouse_x, mouse_y); - return ret; + int ret; + gui->logic(); + gui->draw(); + + if (player) { + dialog_message(player->dialog, MSG_DRAW, 0, 0); + ret = update_dialog(player); + } + + // Draw the mouse + draw_sprite(gui_bitmap, mouse_sprite, mouse_x, mouse_y); + + return ret; } @@ -415,9 +457,17 @@ int gui_load_skin(const char* skinname) { } void gui_exit() { - //alfont_destroy_font(gui_font); - gui_shutdown(); - //alfont_exit(); + delete guiFont; + delete guitop; + delete gui; + + delete input; + delete graphics; + delete imageLoader; + + //alfont_destroy_font(gui_font); + gui_shutdown(); + //alfont_exit(); } void gui_shutdown(void) { diff --git a/src/gui/gui.h b/src/gui/gui.h index e19db25f..a29d02b7 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -29,6 +29,8 @@ #include #include #include +#include +#include typedef struct { BITMAP *grid[9]; @@ -101,6 +103,8 @@ extern LexSkin gui_skin; extern BITMAP *gui_bitmap; extern ALFONT_FONT *gui_font; +extern gcn::Container* guitop; // The top container + /* Definition of the callback function prototypes */ typedef int (*gui_buttonCallback)(int id); typedef char *(*getfuncptr)(int, int *); diff --git a/src/gui/login.cpp b/src/gui/login.cpp index 342ea3dd..0fe39cef 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -22,87 +22,153 @@ */ #include "login.h" +#include "gui.h" #include "../graphic/graphic.h" -/** Display login GUI */ -void login() { -DIALOG login_dialog[] = { - /* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */ - { tmw_dialog_proc, 300, 252, 200, 96, 0, -1, 0, 0, 0, 0, (char*)"Login", NULL, NULL }, - { tmw_text_proc, 304, 284, 50, 10, 0, 0, 0, 0, 0, 0, (char*)"Name:", NULL, NULL }, - { tmw_text_proc, 304, 304, 50, 10, 0, 0, 0, 0, 0, 0,(char*)"Password:", NULL, NULL }, - { tmw_edit_proc, 360, 280, 130, 18, 0, -1, 0, 0, 24, 0, username, NULL, NULL }, - { tmw_password_proc, 360, 300, 130, 18, 0, -1, 0, 0, 24, 0, password, NULL, NULL }, - { tmw_button_proc, 398, 322, 44, 18, 0, -1, 'o', D_EXIT, -1, 0, (char*)"&Ok", NULL, NULL }, - { tmw_button_proc, 446, 322, 44, 18, 0, -1, 'c', D_EXIT, -1, 0, (char*)"&Cancel", NULL, NULL }, - { tmw_check_proc, 304, 322, 60, 18, 0, 0, '1', 0, 0, 0, (char*)"keep", NULL, NULL }, - { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }, -}; - - if(get_config_int("login", "remember", 0)!=0) { - login_dialog[7].flags = D_SELECTED; - if(get_config_string("login", "username", 0)) { - strncpy(username, get_config_string("login", "username", 0), LEN_USERNAME); - username[LEN_USERNAME] = '\0'; - } - else strcpy(username, "player\0"); - } - centre_dialog(login_dialog); - DIALOG_PLAYER *player = init_dialog(login_dialog, -1); - int gui_exit = 1; - while ((!key[KEY_ESC])&&(gui_exit)&&(state!=EXIT)&&(!key[KEY_ENTER])) { - clear_bitmap(buffer); - blit((BITMAP *)graphic[LOGIN_BMP].dat, buffer, 0, 0, 0, 0, 800, 600); - gui_exit = gui_update(player); - blit(buffer, screen, 0, 0, 0, 0, 800, 600); - } - state = EXIT; - set_config_int("login", "remember", (login_dialog[7].flags & D_SELECTED)>>1); - if(login_dialog[7].flags & D_SELECTED) { - if(!username)strcpy(username, "player\0"); - set_config_string("login", "username", username); - } else set_config_string("login", "username", "player\0"); - log("Network", "Username is %s", username); - gui_exit = shutdown_dialog(player); - if((gui_exit==5)||(key[KEY_ENTER])) { - if(username[0]=='\0') { +// Dialog parts +gcn::Container *dialog; +gcn::Label *userLabel; +gcn::Label *passLabel; +gcn::TextField *userField; +gcn::TextField *passField; +gcn::CheckBox *keepCheck; +gcn::Button *okButton; +gcn::Button *cancelButton; + +void LoginActionListener::action(const std::string& eventId) +{ + if (eventId == "ok") { + const std::string user = userField->getText(); + log("Network", "Username is %s", user.c_str()); + + // Store config settings + set_config_int("login", "remember", keepCheck->isMarked()); + if (keepCheck->isMarked()) { + set_config_string("login", "username", user.c_str()); + } else { + set_config_string("login", "username", ""); + } + + // Check login + if (user.length() == 0) { ok("Error", "Enter your username first"); warning("Enter your username first"); state = LOGIN; } else { - server_login(); + server_login(user, passField->getText()); close_session(); } - } + } else if (eventId == "cancel") { + state = EXIT; + } +} + +/* + * Display login GUI + */ +void login() { + // Create dialog + dialog = new gcn::Container(); + userLabel = new gcn::Label("Name:"); + passLabel = new gcn::Label("Password:"); + userField = new gcn::TextField("player"); + passField = new gcn::TextField(); + keepCheck = new gcn::CheckBox("Keep", false); + okButton = new gcn::Button("OK"); + cancelButton = new gcn::Button("Cancel"); + + dialog->setDimension(gcn::Rectangle(300, 250, 200, 80)); + userLabel->setPosition(4, 14); + passLabel->setPosition(4, 34); + userField->setPosition(60, 10); + passField->setPosition(60, 30); + userField->setWidth(130); + passField->setWidth(130); + keepCheck->setPosition(4, 52); + keepCheck->setMarked(get_config_int("login", "remember", 0)); + okButton->setPosition(120, 52); + cancelButton->setPosition(146, 52); + + userField->setEventId("ok"); + passField->setEventId("ok"); + keepCheck->setEventId("ok"); + okButton->setEventId("ok"); + cancelButton->setEventId("cancel"); + + LoginActionListener *loginActionListener = new LoginActionListener(); + userField->addActionListener(loginActionListener); + passField->addActionListener(loginActionListener); + keepCheck->addActionListener(loginActionListener); + okButton->addActionListener(loginActionListener); + cancelButton->addActionListener(loginActionListener); + + dialog->add(userLabel); + dialog->add(passLabel); + dialog->add(userField); + dialog->add(passField); + dialog->add(keepCheck); + dialog->add(okButton); + dialog->add(cancelButton); + + guitop->add(dialog); + + if (get_config_int("login", "remember", 0)) { + if (get_config_string("login", "username", 0)) { + userField->setText(get_config_string("login", "username", "")); + } + } + + userField->requestFocus(); + userField->setCaretPosition(userField->getText().length()); + + while (state == LOGIN) { + clear_bitmap(buffer); + blit((BITMAP *)graphic[LOGIN_BMP].dat, buffer, 0, 0, 0, 0, 800, 600); + gui_update(NULL); + blit(buffer, screen, 0, 0, 0, 0, 800, 600); + if (key[KEY_ESC]) { + state = EXIT; + } + } + + delete dialog; + delete userLabel; + delete passLabel; + delete userField; + delete passField; + delete keepCheck; + delete okButton; + delete cancelButton; } /** Attempt to login to login server */ -void server_login() { - int ret; +void server_login(const std::string& user, const std::string& pass) { + strncpy(username, user.c_str(), LEN_USERNAME); + strncpy(password, pass.c_str(), LEN_PASSWORD); + int ret; // Connect to login server - ret = open_session(get_config_string("server", "host", 0), get_config_int("server", "port", 0)); - if(ret==SOCKET_ERROR) { - state = LOGIN; - ok("Error", "Unable to connect to login server"); - warning("Unable to connect to login server"); - return; - } - - // Send login infos + ret = open_session(get_config_string("server", "host", 0), get_config_int("server", "port", 0)); + if (ret == SOCKET_ERROR) { + state = LOGIN; + ok("Error", "Unable to connect to login server"); + warning("Unable to connect to login server"); + return; + } + // Send login infos - WFIFOW(0) = net_w_value(0x0064); - - WFIFOL(2) = 0; - memcpy(WFIFOP(6), username, 24); - memcpy(WFIFOP(30), password, 24); - WFIFOB(54) = 0; - WFIFOSET(55); - - while((in_size<23)||(out_size>0))flush(); - log("Network", "Packet ID: %x", RFIFOW(0)); - log("Network", "Packet length: %d", get_packet_length(RFIFOW(0))); + WFIFOW(0) = net_w_value(0x0064); + + WFIFOL(2) = 0; + memcpy(WFIFOP(6), username, 24); + memcpy(WFIFOP(30), password, 24); + WFIFOB(54) = 0; + WFIFOSET(55); + + while((in_size<23)||(out_size>0))flush(); + log("Network", "Packet ID: %x", RFIFOW(0)); + log("Network", "Packet length: %d", get_packet_length(RFIFOW(0))); if(RFIFOW(0)==0x0069) { while(in_size0; + bool ret = (tiled_map.tiles[x_c][y_c].data[3] & 0x0002)>0; if(ret==true) { NODE *node = get_head(); while(node && ret==true) { @@ -73,9 +73,9 @@ unsigned char get_path_walk(unsigned short x, unsigned short y) { /** Tell if a tile is animated or not */ bool get_anim(short x_c, short y_c, char layer) { - char temp = map.tiles[x_c][y_c].flags & 0x00C0; + char temp = tiled_map.tiles[x_c][y_c].flags & 0x00C0; temp>>=6; - if(abs(temp)==layer)return (map.tiles[x_c][y_c].data[3] & 0x0001)>0; + if(abs(temp)==layer)return (tiled_map.tiles[x_c][y_c].data[3] & 0x0001)>0; else return false; } @@ -83,21 +83,21 @@ bool get_anim(short x_c, short y_c, char layer) { void set_tile(short x_c, short y_c, char layer, unsigned short id) { if(layer==0) { id <<= 6; - map.tiles[x_c][y_c].data[0] = HIBYTE(id); - map.tiles[x_c][y_c].data[1] &= 0x003f; - map.tiles[x_c][y_c].data[1] |= LOBYTE(id); + tiled_map.tiles[x_c][y_c].data[0] = HIBYTE(id); + tiled_map.tiles[x_c][y_c].data[1] &= 0x003f; + tiled_map.tiles[x_c][y_c].data[1] |= LOBYTE(id); } else if(layer==1) { id <<= 4; - map.tiles[x_c][y_c].data[1] &= 0x00c0; - map.tiles[x_c][y_c].data[1] |= HIBYTE(id); - map.tiles[x_c][y_c].data[2] &= 0x000f; - map.tiles[x_c][y_c].data[2] |= LOBYTE(id); + tiled_map.tiles[x_c][y_c].data[1] &= 0x00c0; + tiled_map.tiles[x_c][y_c].data[1] |= HIBYTE(id); + tiled_map.tiles[x_c][y_c].data[2] &= 0x000f; + tiled_map.tiles[x_c][y_c].data[2] |= LOBYTE(id); } else if(layer==2) { id <<= 2; - map.tiles[x_c][y_c].data[2] &= 0x00f0; - map.tiles[x_c][y_c].data[2] |= HIBYTE(id); - map.tiles[x_c][y_c].data[3] &= 0x0003; - map.tiles[x_c][y_c].data[3] |= LOBYTE(id); + tiled_map.tiles[x_c][y_c].data[2] &= 0x00f0; + tiled_map.tiles[x_c][y_c].data[2] |= HIBYTE(id); + tiled_map.tiles[x_c][y_c].data[3] &= 0x0003; + tiled_map.tiles[x_c][y_c].data[3] |= LOBYTE(id); } } @@ -105,13 +105,16 @@ void set_tile(short x_c, short y_c, char layer, unsigned short id) { unsigned short get_tile(short x_c, short y_c, char layer) { unsigned short id; if(layer==0) { - id = MAKEWORD(map.tiles[x_c][y_c].data[1] & 0x00c0, map.tiles[x_c][y_c].data[0]); + id = MAKEWORD(tiled_map.tiles[x_c][y_c].data[1] & 0x00c0, + tiled_map.tiles[x_c][y_c].data[0]); id >>= 6; } else if(layer==1) { - id = MAKEWORD(map.tiles[x_c][y_c].data[2] & 0x00f0, map.tiles[x_c][y_c].data[1] & 0x003f); + id = MAKEWORD(tiled_map.tiles[x_c][y_c].data[2] & 0x00f0, + tiled_map.tiles[x_c][y_c].data[1] & 0x003f); id >>= 4; } else if(layer==2) { - id = MAKEWORD(map.tiles[x_c][y_c].data[3] & 0x00fc, map.tiles[x_c][y_c].data[2] & 0x000f); + id = MAKEWORD(tiled_map.tiles[x_c][y_c].data[3] & 0x00fc, + tiled_map.tiles[x_c][y_c].data[2] & 0x000f); id >>=2; } return id; diff --git a/src/map.h b/src/map.h index ff9b6309..02dba1c3 100644 --- a/src/map.h +++ b/src/map.h @@ -19,8 +19,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef _MAP_H -#define _MAP_H +#ifndef _TMW_MAP_H +#define _TMW_MAP_H #define MAP_WIDTH 200 #define MAP_HEIGHT 200 -- cgit v1.2.3-70-g09d2