Advertisement
Guest User

App.cpp

a guest
Jun 4th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.35 KB | None | 0 0
  1. //==============================================================================
  2. #include "App.h"
  3.  
  4. namespace LIB {
  5.  
  6.  
  7.  
  8. //==============================================================================
  9.     App::App() :
  10.         window(),
  11.         inp() {
  12.  
  13.         // Initialize GLEW
  14.         glewExperimental = true; // Needed for core profile
  15.         if (glewInit() != GLEW_OK) {
  16.             fprintf(stderr, "Failed to initialize GLEW\n");
  17.         }
  18.  
  19.         Initialized = false;
  20.  
  21.         anim = 0;
  22.         frame = 0;
  23.         frames = 8;
  24.         dir = 0;
  25.         dirs = 8;
  26.  
  27.         anim_sp = 0.03;
  28.         anim_n = 0.0;
  29.  
  30.         speed = 0.014;
  31.         velX = velY = 0;
  32.         double ax[8] = { 4.0, 2.5, 0.0, -2.5, -4.0, -2.5, 0.0, 2.5 };
  33.         double ay[8] = { 0.0, -2.0, -3.0, -2.0, 0.0, 2.0, 3.0, 2.0 };
  34.         for( int a=0; a<8; ++a ) {
  35.             accX[a] = ax[a];
  36.             accY[a] = ay[a];
  37.         }
  38.         friX = friY = 0.8;
  39.  
  40.         UP = DOWN = RIGHT = LEFT = false;
  41.  
  42.         sf::Keyboard::Key keys[] = {
  43.             sf::Keyboard::A,
  44.             sf::Keyboard::W,
  45.             sf::Keyboard::D,
  46.             sf::Keyboard::S,
  47.             sf::Keyboard::Up,
  48.             sf::Keyboard::Left,
  49.             sf::Keyboard::Down,
  50.             sf::Keyboard::Right,
  51.             sf::Keyboard::Q,
  52.             sf::Keyboard::E,
  53.             sf::Keyboard::Return,
  54.             sf::Keyboard::Tab
  55.         };
  56.         inp.set(keys);
  57.  
  58.         /////////
  59.  
  60.         x = 100.0;
  61.         y = 100.0;
  62.         w = 40.0;
  63.         h = 57.0;
  64.  
  65.         offsetX = -x;
  66.         offsetY = -y;
  67.  
  68.         //opengl etc
  69.  
  70.         camdir = 0.0;
  71.         camdir_ang = camdir*180.0/3.1415;
  72.  
  73.  
  74.  
  75.         game = new CORE::Game();
  76.         painter = new Painter();
  77.  
  78.     }
  79.  
  80.     App::~App() {
  81.  
  82.         delete painter;
  83.         delete game;
  84.         Cleanup();
  85.  
  86.     }
  87.  
  88.     sf::RenderWindow* App::get_window() { return &window; }
  89.  
  90. //------------------------------------------------------------------------------
  91.     void App::Input() {
  92.  
  93.         if(Initialized) {
  94.  
  95.             // handle events
  96.             sf::Event event;
  97.             while (window.pollEvent(event))
  98.             {
  99.                 inp.sync(event);
  100.                 if (event.type == sf::Event::Closed)
  101.                 {
  102.                     // end the program
  103.                     Running = false;
  104.                 }
  105.             }
  106.  
  107.         }
  108.  
  109.     }
  110.  
  111. //------------------------------------------------------------------------------
  112.     bool App::Init() {
  113.  
  114.         sf::ContextSettings settings = window.getSettings();
  115.         printf("OpenGL version: %i.%i\n", settings.majorVersion, settings.minorVersion);
  116.  
  117.         glViewport(0,0,window.getSize().x, window.getSize().y); // Render on the whole framebuffer, complete from the lower left corner to the upper right
  118.  
  119.  
  120.  
  121.         // Dark blue background
  122.         glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
  123.  
  124.         glDisable(GL_CULL_FACE);
  125.  
  126.         glEnable(GL_TEXTURE_2D);
  127.         // Z-buffer read and write
  128.         glDisable(GL_DEPTH_TEST);
  129.         // Lighting
  130.         glDisable(GL_LIGHTING);
  131.  
  132.         // Alpha channel (transparency)
  133.         glEnable(GL_BLEND);
  134.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  135.  
  136.  
  137.         //glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
  138.         //glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
  139.  
  140.         //glViewport(0,0,getSize().x, getSize().y); // Render on the whole framebuffer, complete from the lower left corner to the upper right
  141.  
  142.         glMatrixMode(GL_PROJECTION);
  143.         glLoadIdentity();
  144.         glOrtho(0, window.getSize().x, window.getSize().y, 0, -1, 1);
  145.         glMatrixMode(GL_MODELVIEW);
  146.         glLoadIdentity();
  147.  
  148.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  149.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  150.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  151.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  152.  
  153.  
  154.  
  155.  
  156.         // Core
  157.         Initialized = painter->init(game->get_drawn());
  158.         return Initialized;
  159.     }
  160.  
  161. //------------------------------------------------------------------------------
  162.     void App::Loop() {
  163.  
  164.  
  165.         inp.frame();
  166.         UP = inp.MOVE_UP();
  167.         DOWN = inp.MOVE_DOWN();
  168.         LEFT = inp.MOVE_LEFT();
  169.         RIGHT = inp.MOVE_RIGHT();
  170.  
  171.  
  172.  
  173.         // NORTH
  174.         if(UP && !DOWN) {
  175.  
  176.             // NORTH WEST
  177.             if(LEFT && !RIGHT) {
  178.                 dir = 3;
  179.             }
  180.             // NORTH EAST
  181.             else if(!LEFT && RIGHT) {
  182.                 dir = 1;
  183.             }
  184.             else dir = 2;
  185.         }
  186.         // SOUTH
  187.         else if(!UP && DOWN) {
  188.  
  189.  
  190.             // SOUTH WEST
  191.             if(LEFT && !RIGHT) {
  192.                 dir = 5;
  193.             }
  194.             // SOUTH EAST
  195.             else if(!LEFT && RIGHT) {
  196.                 dir = 7;
  197.             }
  198.             else dir = 6;
  199.         }
  200.         // WEST
  201.         else if(LEFT && !RIGHT) {
  202.             dir = 4;
  203.         }
  204.         // EAST
  205.         else if(!LEFT && RIGHT) {
  206.             dir = 0;
  207.         }
  208.  
  209.         // MOVE
  210.         if(LEFT || RIGHT || UP || DOWN) {
  211.             velX += speed*accX[dir];
  212.             velY += speed*accY[dir];
  213.             anim = 1;
  214.             frames = 8;
  215.         } else {
  216.             anim = 0;
  217.             frames = 1;
  218.         }
  219.         velX *= friX;
  220.         velY *= friY;
  221.  
  222.         x += velX;
  223.         y += velY;
  224.  
  225.         game->update();
  226.  
  227.         offsetX = x;
  228.         offsetY = y;
  229.         //if(offsetX<0.0) offsetX = 0.0;
  230.         //if(offsetY<0.0) offsetY = 0.0;
  231.  
  232.         anim_n += anim_sp;
  233.         frame = int(anim_n)%frames;
  234.  
  235.     }
  236.  
  237. //------------------------------------------------------------------------------
  238.     void App::Render() { Render(offsetX, offsetY); }
  239.     void App::Render(int ox, int oy) {
  240.  
  241.  
  242.  
  243.         if(Initialized) {
  244.             window.setActive();
  245.  
  246.             // OpenGL
  247.  
  248.             //glClear( GL_COLOR_BUFFER_BIT );
  249.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  250.             glViewport(0,0,window.getSize().x, window.getSize().y); // Render on the whole framebuffer, complete from the lower left corner to the upper right
  251.  
  252.             painter->paint(-ox, -oy, window.getSize().x, window.getSize().y);
  253.  
  254.             // Swap buffers
  255.             window.display();
  256.  
  257.         }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.     }
  264.  
  265. //------------------------------------------------------------------------------
  266.     void App::Cleanup() {
  267.  
  268.  
  269.         ///painter->cleanup();
  270.         if(window.isOpen()) window.close();
  271.         Initialized = false;
  272.  
  273.  
  274.     }
  275.  
  276.     //------------------------------------------------------------------------------
  277.     bool App::IsRunning() {
  278.  
  279.         return Running;
  280.     }
  281.     //------------------------------------------------------------------------------
  282.     bool App::IsInitialized() {
  283.  
  284.         return Initialized;
  285.     }
  286.  
  287.     //------------------------------------------------------------------------------
  288.     void App::SetCamera(int ox, int oy) {
  289.  
  290.         offsetX = (double)ox;
  291.         offsetY = (double)oy;
  292.     }
  293.  
  294.     //------------------------------------------------------------------------------
  295.     void App::Terminate() {
  296.  
  297.         Running = false;
  298.     }
  299.  
  300.     //------------------------------------------------------------------------------
  301.     void App::Execute(bool edit, sf::WindowHandle context) {
  302.  
  303.         Cleanup();
  304.  
  305.  
  306.         sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
  307.         sf::VideoMode mode(480, 270, desktop.bitsPerPixel);
  308.  
  309.         // Request a 32-bits depth buffer when creating the window
  310.         sf::ContextSettings contextSettings;
  311.         contextSettings.depthBits = desktop.bitsPerPixel;
  312.         contextSettings.majorVersion = 3;
  313.         contextSettings.minorVersion = 3;
  314.         contextSettings.antialiasingLevel = 0;
  315.  
  316.         // SFML
  317.         if(edit) window.create(context, contextSettings);
  318.         else window.create(mode, "Witch Hunt", sf::Style::Default, contextSettings);
  319.  
  320.  
  321.         //Initialized = false;
  322.  
  323.     }
  324.  
  325.     //==============================================================================
  326.  
  327.     int App::GetWindowWidth()  { return WindowWidth; }
  328.     int App::GetWindowHeight() { return WindowHeight; }
  329.  
  330.  
  331.     //==============================================================================
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement