int main (int argc, char *argv[]) { /*init critical variables*/ bServerOnline = false; bConsoleActive = false; /*init resources*/ GtkWidget *window; /*init threads*/ g_thread_init(NULL); gdk_threads_init(); gdk_threads_enter(); add_console_msg("[Lucky",motd, "SoftServer Build "+convert_to_str(BUILD_NUM)+"\nhttp://www.softserver.org\n\nAre you still using the trial version?\nDevelop an app using SoftServer and submit it to earn a license!"); add_console_msg("[Main]",notification,"Initializing GTK+ v2.0"); /*init GTK*/ gtk_init(&argc, &argv); /*Window setup*/ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW (window), std::string("SoftServer Build "+convert_to_str(BUILD_NUM)).c_str()); gtk_window_set_default_size(GTK_WINDOW(window), 550, 350); gtk_window_set_resizable(GTK_WINDOW(window), false); /*Console*/ console_text = gtk_text_view_new(); //Start console up console_buffer = gtk_text_view_get_buffer((GtkTextView*)console_text); while(!start_console_format()); //Start up formats gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(console_text), GTK_WRAP_WORD); gtk_text_view_set_editable(GTK_TEXT_VIEW(console_text), false); gtk_widget_set_size_request(console_text, 595, 345); GdkColor console_color_bg; console_color_bg.red = 0; console_color_bg.green = 0; console_color_bg.blue = 0; gtk_widget_modify_base(console_text, GTK_STATE_NORMAL, &console_color_bg); /*Label setup*/ status_msg = gtk_label_new(NULL); change_status_msg("SoftServer <i>Build "+convert_to_str(BUILD_NUM)+"</i>"); start_and_stop_button = gtk_button_new_with_label("Start"); fixed_location = gtk_fixed_new(); gtk_fixed_put(GTK_FIXED(fixed_location), console_text,0,0); gtk_fixed_put(GTK_FIXED(fixed_location), status_msg, 5, 360); gtk_fixed_put(GTK_FIXED(fixed_location), start_and_stop_button, 550, 350); gtk_container_add(GTK_CONTAINER(window), fixed_location); /*Show widgets here*/ gtk_container_set_border_width(GTK_CONTAINER(window), 5); gtk_widget_show_all(window); add_console_msg("[Main]", notification,"Waiting for start button to be pressed"); start_stop_button_ls_id = g_signal_connect_after(start_and_stop_button, "released", G_CALLBACK(init_serv), NULL); g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL); activate_console(); /*Exit*/ gtk_main(); gdk_threads_leave(); return 0; }
void Game::run() { #ifdef TEST_NASTY_CONDITIONS Random random; const float time_scale = NASTY_MIN_RATE + (NASTY_MAX_RATE - NASTY_MIN_RATE) * random.frand_lte(); Time::Second_Type time_used = Time::Second_Type(); Time start_time; #endif Sound_Source_Pool &sspr = get_Sound_Source_Pool(); Time time_processed; for(;;) { const Time time_passed; float time_step = time_passed.get_seconds_since(time_processed); time_processed = time_passed; #ifdef ENABLE_XINPUT get_Joysticks().poll(); #endif if(joy_mouse.enabled && (joy_mouse.velocity.x != 0 || joy_mouse.velocity.y != 0)) { Point2f adjusted_vel(joy_mouse.velocity.x + 0.5f, joy_mouse.velocity.y + 0.5f); if(adjusted_vel.x < 0.0f) adjusted_vel.x = std::min(0.0f, adjusted_vel.x + joy_mouse.noise_zone.x); else adjusted_vel.x = std::max(0.0f, adjusted_vel.x - joy_mouse.noise_zone.x); if(adjusted_vel.y < 0.0f) adjusted_vel.y = std::min(0.0f, adjusted_vel.y + joy_mouse.noise_zone.y); else adjusted_vel.y = std::max(0.0f, adjusted_vel.y - joy_mouse.noise_zone.y); adjusted_vel.x /= 32767.5f - joy_mouse.noise_zone.x; adjusted_vel.y /= 32767.5f - joy_mouse.noise_zone.y; int xrel = int(adjusted_vel.x * joy_mouse.pixels_per_second.x * time_step); int yrel = int(adjusted_vel.y * joy_mouse.pixels_per_second.y * time_step); if(xrel || yrel) { int x, y; SDL_GetMouseState(&x, &y); x += xrel; y += yrel; if(x < 0) x = 0; else if(x >= get_Window().get_width()) x = get_Window().get_width() - 1; if(y < 0) y = 0; else if(y >= get_Window().get_width()) y = get_Window().get_height() - 1; SDL_WarpMouse(Uint16(x), Uint16(y)); } } for(SDL_Event event; SDL_PollEvent(&event);) { if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { const SDL_keysym &s = event.key.keysym; const bool alt = get_key_state(SDLK_LALT) || get_key_state(SDLK_RALT); const bool ctrl = get_key_state(SDLK_LCTRL) || get_key_state(SDLK_RCTRL); const bool shift = get_key_state(SDLK_LSHIFT) || get_key_state(SDLK_RSHIFT); const bool gui = #if SDL_VERSION_ATLEAST(1,3,0) get_key_state(SDLK_LGUI) || get_key_state(SDLK_RGUI); #else get_key_state(SDLK_LMETA) || get_key_state(SDLK_RMETA) || get_key_state(SDLK_LSUPER) || get_key_state(SDLK_RSUPER); #endif #ifndef NDEBUG if(s.sym == SDLK_BACKQUOTE && alt && !ctrl && !gui && !shift) { if(event.type == SDL_KEYDOWN) { if(m_console_active) deactivate_console(); else activate_console(); } continue; } #endif on_event(event); if(event.type == SDL_KEYDOWN && ( #if defined(_MACOSX) (!alt && !ctrl && gui && !shift && s.sym == SDLK_q) || #endif (!alt && ctrl && !gui && !shift && s.sym == SDLK_q) || ( alt && !ctrl && !gui && !shift && s.sym == SDLK_F4))) { throw Quit_Event(); } } #if SDL_VERSION_ATLEAST(1,3,0) else if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) { on_event(event); if(event.window.windowID == SDL_GetWindowID(get_Window().get_window())) { get_Window().alert_window_destroyed(); throw Quit_Event(); } } #endif else if(event.type == SDL_JOYAXISMOTION) { if(joy_mouse.enabled && (joy_mouse.joy_axes.x == event.jaxis.axis || joy_mouse.joy_axes.y == event.jaxis.axis)) { if(joy_mouse.joy_axes.x == event.jaxis.axis) joy_mouse.velocity.x = event.jaxis.value; else joy_mouse.velocity.y = event.jaxis.value; } else on_event(event); } else if(event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) { #ifdef _MACOSX if(event.jbutton.which < 4 && event.jbutton.button < 4) { static bool values[4][4] = {{false, false, false, false}, {false, false, false, false}, {false, false, false, false}, {false, false, false, false}}; values[event.jbutton.which][event.jbutton.button] = event.jbutton.state == SDL_PRESSED; SDL_Event e; e.type = Uint8(SDL_JOYHATMOTION); e.jhat.which = event.jbutton.which; e.jhat.hat = 0; if(values[event.jbutton.which][0]) { if(values[event.jbutton.which][2]) e.jhat.value = SDL_HAT_LEFTUP; else if(values[event.jbutton.which][3]) e.jhat.value = SDL_HAT_RIGHTUP; else e.jhat.value = SDL_HAT_UP; } else if(values[event.jbutton.which][1]) { if(values[event.jbutton.which][2]) e.jhat.value = SDL_HAT_LEFTDOWN; else if(values[event.jbutton.which][3]) e.jhat.value = SDL_HAT_RIGHTDOWN; else e.jhat.value = SDL_HAT_DOWN; } else { if(values[event.jbutton.which][2]) e.jhat.value = SDL_HAT_LEFT; else if(values[event.jbutton.which][3]) e.jhat.value = SDL_HAT_RIGHT; else e.jhat.value = SDL_HAT_CENTERED; } SDL_PushEvent(&e); } else #endif if(joy_mouse.enabled && joy_mouse.left_click == event.jbutton.button) { SDL_Event e; e.type = Uint8(event.type == SDL_JOYBUTTONDOWN ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP); e.button.which = Uint8(event.jbutton.which + 1); e.button.state = event.jbutton.state; e.button.button = SDL_BUTTON_LEFT; int x, y; SDL_GetMouseState(&x, &y); e.button.x = Uint16(x); e.button.y = Uint16(y); on_event(e); } else if(joy_mouse.enabled && joy_mouse.escape == event.jbutton.button) { SDL_Event e; e.type = Uint8(event.type == SDL_JOYBUTTONDOWN ? SDL_KEYDOWN : SDL_KEYUP); e.key.which = Uint8(event.jbutton.which + 1); e.key.state = event.jbutton.state; e.key.keysym.mod = SDL_GetModState(); e.key.keysym.scancode = 0; e.key.keysym.sym = SDLK_ESCAPE; e.key.keysym.unicode = 0; on_event(e); } else on_event(event); } else if(event.type == SDL_JOYHATMOTION) { if(joy_mouse.enabled && joy_mouse.scroll_hat == event.jhat.hat && (event.jhat.value == SDL_HAT_DOWN || event.jhat.value == SDL_HAT_UP)) { SDL_Event e; e.type = SDL_MOUSEBUTTONDOWN; e.button.which = Uint8(event.jhat.which + 1); e.button.state = SDL_PRESSED; e.button.button = Uint8(event.jhat.value == SDL_HAT_DOWN ? SDL_BUTTON_WHEELDOWN : SDL_BUTTON_WHEELUP); int x, y; SDL_GetMouseState(&x, &y); e.button.x = Uint16(x); e.button.y = Uint16(y); on_event(e); e.type = SDL_MOUSEBUTTONUP; e.button.state = SDL_RELEASED; on_event(e); } else on_event(event); } else { on_event(event); if(event.type == SDL_QUIT) throw Quit_Event(); } } #ifdef TEST_NASTY_CONDITIONS { const Time current_time; const Time::Second_Type time_passed = time_scale * current_time.get_seconds_since(start_time); size_t step_count = 0u; while(time_used + (1 / 60.0f) < time_passed) { time_used += (1 / 60.0f); perform_logic(); if(++step_count == NASTY_RATE_CUTOFF) time_used = time_passed; } if(!random.rand_lt(NASTY_ZERO_STEP_FREQUENCY)) perform_logic(); } #else perform_logic(); #endif sspr.update(); if(Window::is_enabled()) { Video &vr = get_Video(); if(vr.begin_prerender()) { prerender(); if(vr.begin_render()) { try { render(); } catch(...) { vr.end_render(); throw; } vr.end_render(); } } } } }
bool init_serv() { if(!bServerOnline) //Make sure it is offline. { activate_console(); gtk_widget_set_sensitive(start_and_stop_button, false); /*Gray it out*/ init_args xml_args = load_external_files(); if(!xml_args.bSuccess) { gtk_widget_set_sensitive(start_and_stop_button, true); add_console_msg("[Server]",error,"has ended init_serv() process"); return false; //End server start up right away } bool bErrorFree = true; for(unsigned int i=0; i<sizeof(bThreadActive)/sizeof(bool); i++) //Start all threads false { bThreadActive[i] = false; } uiMasterSocketTCP = socket(AF_INET, SOCK_STREAM, 0); if(uiMasterSocketTCP < 0) { perror("tcp-socket"); add_console_msg("[Init]",error,"Could not initialize TCP socket"); return false; } uiMasterSocketUDP = socket(AF_INET, SOCK_DGRAM, 0); if(uiMasterSocketUDP < 0) { perror("udp-socket"); add_console_msg("[Init]",error,"Could not initialize UDP socket"); return false; } memset(&SBindTCPAddress, 0, sizeof(SBindTCPAddress)); //Memset to make sure memset(&SBindUDPAddress, 0, sizeof(SBindUDPAddress)); //Memset to make sure SBindTCPAddress.sin_family = AF_INET; SBindUDPAddress.sin_family = AF_INET; SBindTCPAddress.sin_port = htons(xml_args.uiPortTCP); SBindUDPAddress.sin_port = htons(xml_args.uiPortUDP); SBindTCPAddress.sin_addr.s_addr = INADDR_ANY; SBindUDPAddress.sin_addr.s_addr = INADDR_ANY; if(bind(uiMasterSocketTCP, (struct sockaddr*) &SBindTCPAddress, sizeof SBindTCPAddress) < 0) { perror("tcp-bind"); //Perrors are only for us to debug add_console_msg("[Init]",error, "Could not bind TCP"); //These are for the end-user bErrorFree = false; return false; } if(bind(uiMasterSocketUDP, (struct sockaddr*) &SBindUDPAddress, sizeof SBindUDPAddress) < 0) { perror("udp-bind"); add_console_msg("[Init]",error, "Could not bind UDP"); bErrorFree = false; return false; } if(listen(uiMasterSocketTCP, xml_args.uiMaxQueue) < 0) { perror("tcp-listen"); add_console_msg("[Init]",error, "Could not listen on TCP socket"); bErrorFree = false; return false; } bServerOnline = bErrorFree; if(bErrorFree) { /*listen thread*/ thread_args listen_thread_args; pthread_create(&listen_thread, NULL, listen_thread_func, &listen_thread_args); /*flow thread*/ thread_args flow_thread_args; pthread_create(&flow_thread, NULL, flow_thread_func, &flow_thread_args); thread_args save_thread_args; pthread_create(&save_thread, NULL, save_thread_func, &save_thread_args); while(true) { poll(0,0, CPU_DELAY); if(check_if_all_threads_running()) { gtk_widget_set_sensitive(start_and_stop_button, true); /*ungray*/ gtk_button_set_label((GtkButton*)start_and_stop_button, "Stop"); g_signal_handler_disconnect(start_and_stop_button, start_stop_button_ls_id); start_stop_button_ls_id = g_signal_connect_after(start_and_stop_button, "released", G_CALLBACK(stop_serv), NULL); change_status_msg("<span foreground='green'><b>Online</b></span> <b>0</b> user(s) connected"); add_console_msg("[Server]", important_online, "[Server is now online]"); break; } } return true; } } return false; }