Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Setup : CodeBlocks 16.01 + Allegro 5.0.10 + gcc 4.9.2 tdm
- //-------------------------------------------------------------------------------------------------------------------------------------
- -static -lstdc++
- -static -lgcc
- -lboost_system-mgw47-mt-1_58
- -lboost_filesystem-mgw47-mt-1_58
- -lallegro-5.0.10-monolith-static-mt
- -lFLAC-1.2.1-static-mt
- -lfreetype-2.4.8-static-mt
- -lallegro_acodec-5.0.10-static-mt
- -lallegro_audio-5.0.10-static-mt
- -lvorbisfile-1.3.2-static-mt
- -lvorbis-1.3.2-static-mt
- -ldumb-0.9.3-static-mt
- -logg-1.2.1-static-mt
- -lopenal-1.14-static-mt
- -lwinmm
- -lgdi32
- -luser32
- -lkernel32
- -lopengl32
- -ldxguid
- -ldinput8
- -ld3d9
- -lpsapi
- -lpng
- -ljpeg
- -lz
- -lole32
- -ld3dx9d
- -ld3dim
- -ld3drm
- -lgdiplus
- -ld3dxof
- -lws2_32
- -lcomdlg32
- -lshlwapi
- -lstdc++
- -luuid
- //-------------------------------------------------------------------------------------------------------------------------------------
- // origin.h
- //-------------------------------------------------------------------------------------------------------------------------------------
- #ifndef ORIGIN_H
- #define ORIGIN_H
- //{
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_ttf.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_image.h>
- #include <allegro5/allegro_native_dialog.h>
- #include <allegro5/allegro_audio.h>
- #include <allegro5/allegro_acodec.h>
- #include <allegro5/allegro_windows.h>
- #include <stdio.h>
- #include <cmath>
- #include <iostream>
- #include <vector>
- #include <boost/filesystem/operations.hpp>
- #include <ctime>
- #define IDM_FILE_NEW 1
- #define IDM_FILE_OPEN 2
- #define IDM_FILE_QUIT 3
- //}
- class Origin
- {
- private:
- // Quit application
- bool quit;
- // Configuration file
- ALLEGRO_CONFIG* mainConfig;
- std::time_t mainConfigTime;
- std::time_t mainConfigLastTime;
- // Screen
- int monitor;
- int screenH;
- int screenW;
- int actualMonitor;
- int nbAdapter;
- bool fullScreen;
- // Window
- int actualMonitorW;
- int actualMonitorH;
- bool stretchScreen;
- bool antiAliasing;
- ALLEGRO_DISPLAY* windowDisplay;
- ALLEGRO_BITMAP* windowBuffer;
- // Events
- ALLEGRO_EVENT_QUEUE* eventQueue;
- // Framerate control
- const float FPS;
- int nbFrame;
- int nbFps;
- double gameTime;
- double oldTime;
- ALLEGRO_TIMER* frameRateTimer;
- // Mouse position
- int mouseXi;
- int mouseYi;
- float mouseXf;
- float mouseYf;
- // Keyboard button on/off
- bool keyAlt;
- bool keyEnter;
- bool keyCtrl;
- bool keyAltEnter;
- ALLEGRO_KEYBOARD_STATE keyState;
- // Resources
- ALLEGRO_TRANSFORM trans;
- ALLEGRO_FONT* mainFont;
- // ALLEGRO_BITMAP * myBmp;
- // ALLEGRO_BITMAP * myBrik;
- // ALLEGRO_BITMAP * mySol;
- // ALLEGRO_BITMAP * pause_img;
- // ALLEGRO_THREAD *thread;
- public:
- //DATA myInput;
- Origin();
- virtual ~Origin();
- int init();
- int done();
- int run();
- int update();
- int input();
- int render();
- int refresh();
- int load();
- int save();
- int calc_framerate();
- int switch_fullScreen(bool isFullscreen);
- void get_desktop_resolution(int adapter, int &w, int &h);
- protected:
- };
- #endif // ORIGIN_H
- //-------------------------------------------------------------------------------------------------------------------------------------
- // origin.cpp
- //-------------------------------------------------------------------------------------------------------------------------------------
- #include "origin.h"
- Origin::Origin(): FPS(60)
- {
- std::cout << "Origin::Origin // Init members !" << std::endl;
- // Quit application
- quit = false;
- // Quit application
- mainConfigTime = 0;
- mainConfigLastTime = 0;
- mainConfig = NULL;
- // Screen
- monitor = 0;
- screenW = 0;
- screenH = 0;
- actualMonitor = 0;
- nbAdapter = 0;
- fullScreen = false;
- // Window
- actualMonitorW = 0;
- actualMonitorH = 0;
- stretchScreen = false;
- antiAliasing = false;
- windowDisplay = NULL;
- windowBuffer = NULL;
- // Events
- eventQueue = NULL;
- // Framerate control
- nbFrame = 0;
- nbFps = 0;
- gameTime = 0;
- oldTime = 0;
- frameRateTimer = NULL;
- // Mouse position
- mouseXi = 0;
- mouseYi = 0;
- mouseXf = 0;
- mouseYf = 0;
- // Keyboard button on/off
- keyAlt = false;
- keyEnter = false;
- keyCtrl = false;
- keyAltEnter = false;
- // Resources
- mainFont = NULL;
- // myBmp = NULL;
- // myBrik = NULL;
- // mySol = NULL;
- // pause_img = NULL;
- // thread = NULL;
- }
- Origin::~Origin()
- {
- std::cout << "Origin::~Origin // Quit application !" << std::endl;
- }
- int Origin::init()
- {
- std::cout << "Origin::init" << std::endl;
- try
- {
- al_set_app_name("Mugen SoftWare");
- if (!al_init()) throw 1;
- if (!al_install_audio()) throw 2;
- if (!al_init_acodec_addon()) throw 3;
- // nombre de son simultannée dans le "Voice" par défaut [default mixer]
- if (!al_reserve_samples(32)) throw 4;
- mainConfig = al_load_config_file ("config.ini");
- stretchScreen = atoi(al_get_config_value(mainConfig, "Display", "stretchScreen"));
- antiAliasing = atoi(al_get_config_value(mainConfig, "Display", "antialiasing"));
- monitor = atoi(al_get_config_value(mainConfig, "Display", "monitor"));
- screenW = atoi(al_get_config_value(mainConfig, "Display", "size_w"));
- screenH = atoi(al_get_config_value(mainConfig, "Display", "size_h"));
- al_set_new_display_adapter(monitor);
- al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_SUGGEST);
- //al_set_new_display_flags(ALLEGRO_OPENGL);
- if (antiAliasing)
- al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1,
- ALLEGRO_SUGGEST |
- ALLEGRO_FULLSCREEN);
- windowDisplay = al_create_display(screenW, screenH);
- if(!windowDisplay) throw 5;
- //--- Init Addons ---
- al_init_font_addon();
- al_init_ttf_addon();
- al_init_image_addon();
- al_init_primitives_addon();
- al_install_keyboard();
- al_install_mouse();
- al_hide_mouse_cursor(windowDisplay);
- eventQueue = al_create_event_queue();
- frameRateTimer = al_create_timer(1.00/FPS);
- al_register_event_source (eventQueue,al_get_timer_event_source(frameRateTimer));
- al_start_timer(frameRateTimer);
- if (Origin::load()) throw 6;
- }
- catch (int error)
- {
- switch (error)
- {
- case 1:
- {
- std::cout << "-- Error at Origin::init() --" <<
- "// al_init : failed to initializing Allegro 5!" << std::endl;
- return -1;
- }
- case 2:
- {
- std::cout << "-- Error at Origin::init() --" <<
- "// al_init : failed to initialize Audio!" << std::endl;
- return -1;
- }
- case 3:
- {
- std::cout << "-- Error at Origin::init() --" <<
- "// al_init : failed to initialize audio codecs!" << std::endl;
- return -1;
- }
- case 4:
- {
- std::cout << "-- Error at Origin::init() --" <<
- "// al_init : failed to reserve samples!" << std::endl;
- return -1;
- }
- case 5:
- {
- std::cout << "-- Error at Origin::init() --" <<
- "// al_init : failed to create windowDisplay!" << std::endl;
- return -1;
- }
- case 6:
- {
- std::cout << "-- Error at Origin::init() --" <<
- "// al_init : failed to load resources!" << std::endl;
- return -1;
- }
- }
- }
- return 0;
- }
- int Origin::done()
- {
- std::cout << "Origin::done" << std::endl;
- al_destroy_font(mainFont);
- al_destroy_event_queue(eventQueue);
- al_destroy_timer(frameRateTimer);
- al_destroy_display(windowDisplay);
- al_destroy_config(mainConfig);
- return 0;
- }
- int Origin::load()
- {
- std::cout << "Origin::load" << std::endl;
- mainFont = al_load_font (al_get_config_value(mainConfig, "Display", "font"),
- atoi(al_get_config_value (mainConfig, "Display", "font_size")),0);
- if (!mainFont)
- {
- printf("Error loading mainFont \n");
- return -2;
- }
- actualMonitor = al_get_new_display_adapter();
- nbAdapter = al_get_num_video_adapters();
- get_desktop_resolution(actualMonitor, actualMonitorW, actualMonitorH);
- windowBuffer = al_create_bitmap(actualMonitorW,actualMonitorH);
- mainConfigTime = boost::filesystem::last_write_time("config.ini");
- mainConfigLastTime = boost::filesystem::last_write_time("config.ini");
- return 0;
- }
- int Origin::save()
- {
- return 0;
- }
- int Origin::run()
- {
- std::cout << "Origin::run" << std::endl;
- while (!quit)
- {
- Origin::input();
- Origin::update();
- Origin::calc_framerate();
- Origin::render();
- }
- return 0;
- }
- int Origin::input()
- {
- int xWindow(0); // Window x position
- int yWindow(0); // Window y position
- al_get_window_position (windowDisplay, &xWindow, &yWindow);
- al_get_mouse_cursor_position(&mouseXi, &mouseYi);
- if (fullScreen)
- {
- mouseXf = float(mouseXi-xWindow) / (float(actualMonitorW)/float(screenW));
- mouseYf = float(mouseYi-yWindow) / (float(actualMonitorH)/float(screenH));
- }
- else
- {
- mouseXf = float(mouseXi-xWindow)-2;
- mouseYf = float(mouseYi-yWindow)-24;
- }
- // Mouse limit zone !
- if (mouseXf>screenW) mouseXf = screenW;
- if (mouseXf<0) mouseXf = 0;
- if (mouseYf>screenH) mouseYf = screenH;
- if (mouseYf<0) mouseYf = 0;
- mouseXi = (int)mouseXf;
- mouseYi = (int)mouseYf;
- ALLEGRO_EVENT event;
- al_wait_for_event(eventQueue, &event);
- al_get_keyboard_state(&keyState);
- if (al_key_down(&keyState, ALLEGRO_KEY_ESCAPE))
- {
- quit = true;
- //myInput.quit = true;
- }
- if (al_key_down(&keyState, ALLEGRO_KEY_F5))
- {
- al_destroy_config(mainConfig);
- mainConfig = al_load_config_file ("config.ini");
- mainFont = al_load_font (al_get_config_value(mainConfig, "Display", "font"),
- atoi(al_get_config_value (mainConfig, "Display", "font_size")),0);
- if (!mainFont)
- {
- printf("Error loading mainFont \n");
- return -2;
- }
- }
- keyAlt = al_key_down(&keyState, ALLEGRO_KEY_ALT);
- keyEnter = al_key_down(&keyState, ALLEGRO_KEY_ENTER);
- if (!keyAlt && !keyEnter) keyAltEnter = false;
- if (keyAlt && keyEnter && !keyAltEnter)
- {
- keyAltEnter = true;
- switch_fullScreen(fullScreen);
- }
- return 0;
- }
- int Origin::update()
- {
- mainConfigTime = boost::filesystem::last_write_time("config.ini");
- if (mainConfigTime != mainConfigLastTime)
- {
- al_destroy_config(mainConfig);
- mainConfig = al_load_config_file ("config.ini");
- mainFont = al_load_font (al_get_config_value(mainConfig, "Display", "font"),
- atoi(al_get_config_value (mainConfig, "Display", "font_size")),0);
- if (!mainFont)
- {
- printf("Error loading mainFont \n");
- return -2;
- }
- mainConfigLastTime = mainConfigTime;
- }
- return 0;
- }
- int Origin::calc_framerate()
- {
- gameTime = al_get_time();
- if (gameTime - oldTime >= 1.0)
- {
- nbFps = nbFrame / (gameTime - oldTime) ;
- nbFrame = 1;
- oldTime = gameTime;
- }
- nbFrame ++;
- return 0;
- }
- int Origin::render()
- {
- al_set_target_bitmap(windowBuffer);
- al_clear_to_color(al_map_rgb(0,62,84));
- // Affichage Texte !
- al_draw_textf(mainFont, al_map_rgb(200,250,250), screenW-60, 10, -1, "FPS: %i", nbFps); // -1 en parametre pour centrer le texte !
- al_draw_text (mainFont, al_map_rgb(155,255,0), 2, screenH-16, 0, "[ Escape ] -> Quit");
- al_draw_text (mainFont, al_map_rgb(255,125,0), 160, screenH-16, 0, "[ Alt+Enter ] -> Switch FullScreen/Windowed");
- // Viseur !
- al_draw_line(mouseXi,0,mouseXi,screenH, al_map_rgb(80, 140, 200), 0);
- al_draw_line(0, mouseYi,screenW, mouseYi, al_map_rgb(80, 140, 200), 0);
- // Dessin du curseur !
- al_draw_line(mouseXi,mouseYi,mouseXi,mouseYi+16, al_map_rgb(200,150,50), 0);
- al_draw_line(mouseXi,mouseYi,mouseXi+16,mouseYi+16, al_map_rgb(200,150,50), 0);
- al_draw_line(mouseXi+4,mouseYi+10,mouseXi+16,mouseYi+16, al_map_rgb(200,150,50), 0);
- al_draw_line(mouseXi+4,mouseYi+10,mouseXi,mouseYi+16, al_map_rgb(200,150,50), 0);
- al_set_target_backbuffer(windowDisplay);
- al_clear_to_color(al_map_rgb(0, 0, 0));
- Origin::refresh();
- return 0;
- }
- int Origin::refresh()
- {
- get_desktop_resolution(actualMonitor, actualMonitorW, actualMonitorH);
- // calculate scaling factor
- int sx = actualMonitorW / screenW;
- int sy = actualMonitorH / screenH;
- int scale = std::min(sx, sy);
- // calculate how much the windowBuffer should be scaled
- float scaleW = screenW * scale;
- float scaleH = screenH * scale;
- float scaleX = (actualMonitorW - scaleW) / 2;
- float scaleY = (actualMonitorH - scaleH) / 2;
- if (fullScreen)
- al_draw_scaled_bitmap(windowBuffer, 0, 0, screenW, screenH,
- scaleX, scaleY, scaleW, scaleH, 0);
- else
- al_draw_scaled_bitmap(windowBuffer, 0, 0, screenW, screenH,
- 1, 1, screenW, screenH, 0);
- al_flip_display();
- return 0;
- }
- int Origin::switch_fullScreen(bool isFullscreen)
- {
- if (!isFullscreen)
- {
- std::cout << "Origin::switch_fullScreen // Fullscreen Mode !" << std::endl;
- al_toggle_display_flag(windowDisplay, ALLEGRO_FULLSCREEN_WINDOW, true);
- fullScreen = true;
- }
- else
- {
- std::cout << "Origin::switch_fullScreen // Windowed Mode !" << std::endl;
- al_toggle_display_flag(windowDisplay, ALLEGRO_FULLSCREEN_WINDOW, false);
- al_identity_transform(&trans);
- al_scale_transform(&trans, 1, 1);
- al_use_transform(&trans);
- fullScreen = false;
- }
- return 0;
- }
- void Origin::get_desktop_resolution(int adapter, int &w, int &h)
- {
- ALLEGRO_MONITOR_INFO info;
- al_get_monitor_info(adapter, &info);
- w = info.x2 - info.x1;
- h = info.y2 - info.y1;
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // main.cpp
- //-------------------------------------------------------------------------------------------------------------------------------------
- #include "origin.h"
- int main()
- {
- Origin myGame;
- try
- {
- if (myGame.init()) throw 1;
- if (myGame.run()) throw 2;
- if (myGame.done()) throw 3;
- }
- catch(int error)
- {
- switch (error)
- {
- case 1:
- {
- std::cout << "-- Error at Origin::init() --" << std::endl;
- }
- case 2:
- {
- std::cout << "-- Error at Origin::run() --" << std::endl;
- }
- case 3:
- {
- std::cout << "-- Error at Origin::done() --" << std::endl;
- }
- }
- }
- return 0;
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // config.ini
- //-------------------------------------------------------------------------------------------------------------------------------------
- #--- Configuration File Allegro 5 Base
- [Display]
- monitor = 0 // Choose the monitor where the game will be windowDisplay
- size_w = 640
- size_h = 480
- stretchScreen = 0
- antialiasing = 0
- framerate = 60
- font_size = 8
- font = kyrou.ttf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement