Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==============================================================================
- #include "App.h"
- namespace LIB {
- //==============================================================================
- App::App() :
- window(),
- inp() {
- // Initialize GLEW
- glewExperimental = true; // Needed for core profile
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- }
- Initialized = false;
- anim = 0;
- frame = 0;
- frames = 8;
- dir = 0;
- dirs = 8;
- anim_sp = 0.03;
- anim_n = 0.0;
- speed = 0.014;
- velX = velY = 0;
- double ax[8] = { 4.0, 2.5, 0.0, -2.5, -4.0, -2.5, 0.0, 2.5 };
- double ay[8] = { 0.0, -2.0, -3.0, -2.0, 0.0, 2.0, 3.0, 2.0 };
- for( int a=0; a<8; ++a ) {
- accX[a] = ax[a];
- accY[a] = ay[a];
- }
- friX = friY = 0.8;
- UP = DOWN = RIGHT = LEFT = false;
- sf::Keyboard::Key keys[] = {
- sf::Keyboard::A,
- sf::Keyboard::W,
- sf::Keyboard::D,
- sf::Keyboard::S,
- sf::Keyboard::Up,
- sf::Keyboard::Left,
- sf::Keyboard::Down,
- sf::Keyboard::Right,
- sf::Keyboard::Q,
- sf::Keyboard::E,
- sf::Keyboard::Return,
- sf::Keyboard::Tab
- };
- inp.set(keys);
- /////////
- x = 100.0;
- y = 100.0;
- w = 40.0;
- h = 57.0;
- offsetX = -x;
- offsetY = -y;
- //opengl etc
- camdir = 0.0;
- camdir_ang = camdir*180.0/3.1415;
- game = new CORE::Game();
- painter = new Painter();
- }
- App::~App() {
- delete painter;
- delete game;
- Cleanup();
- }
- sf::RenderWindow* App::get_window() { return &window; }
- //------------------------------------------------------------------------------
- void App::Input() {
- if(Initialized) {
- // handle events
- sf::Event event;
- while (window.pollEvent(event))
- {
- inp.sync(event);
- if (event.type == sf::Event::Closed)
- {
- // end the program
- Running = false;
- }
- }
- }
- }
- //------------------------------------------------------------------------------
- bool App::Init() {
- sf::ContextSettings settings = window.getSettings();
- printf("OpenGL version: %i.%i\n", settings.majorVersion, settings.minorVersion);
- glViewport(0,0,window.getSize().x, window.getSize().y); // Render on the whole framebuffer, complete from the lower left corner to the upper right
- // Dark blue background
- glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
- glDisable(GL_CULL_FACE);
- glEnable(GL_TEXTURE_2D);
- // Z-buffer read and write
- glDisable(GL_DEPTH_TEST);
- // Lighting
- glDisable(GL_LIGHTING);
- // Alpha channel (transparency)
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- //glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
- //glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
- //glViewport(0,0,getSize().x, getSize().y); // Render on the whole framebuffer, complete from the lower left corner to the upper right
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, window.getSize().x, window.getSize().y, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // Core
- Initialized = painter->init(game->get_drawn());
- return Initialized;
- }
- //------------------------------------------------------------------------------
- void App::Loop() {
- inp.frame();
- UP = inp.MOVE_UP();
- DOWN = inp.MOVE_DOWN();
- LEFT = inp.MOVE_LEFT();
- RIGHT = inp.MOVE_RIGHT();
- // NORTH
- if(UP && !DOWN) {
- // NORTH WEST
- if(LEFT && !RIGHT) {
- dir = 3;
- }
- // NORTH EAST
- else if(!LEFT && RIGHT) {
- dir = 1;
- }
- else dir = 2;
- }
- // SOUTH
- else if(!UP && DOWN) {
- // SOUTH WEST
- if(LEFT && !RIGHT) {
- dir = 5;
- }
- // SOUTH EAST
- else if(!LEFT && RIGHT) {
- dir = 7;
- }
- else dir = 6;
- }
- // WEST
- else if(LEFT && !RIGHT) {
- dir = 4;
- }
- // EAST
- else if(!LEFT && RIGHT) {
- dir = 0;
- }
- // MOVE
- if(LEFT || RIGHT || UP || DOWN) {
- velX += speed*accX[dir];
- velY += speed*accY[dir];
- anim = 1;
- frames = 8;
- } else {
- anim = 0;
- frames = 1;
- }
- velX *= friX;
- velY *= friY;
- x += velX;
- y += velY;
- game->update();
- offsetX = x;
- offsetY = y;
- //if(offsetX<0.0) offsetX = 0.0;
- //if(offsetY<0.0) offsetY = 0.0;
- anim_n += anim_sp;
- frame = int(anim_n)%frames;
- }
- //------------------------------------------------------------------------------
- void App::Render() { Render(offsetX, offsetY); }
- void App::Render(int ox, int oy) {
- if(Initialized) {
- window.setActive();
- // OpenGL
- //glClear( GL_COLOR_BUFFER_BIT );
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glViewport(0,0,window.getSize().x, window.getSize().y); // Render on the whole framebuffer, complete from the lower left corner to the upper right
- painter->paint(-ox, -oy, window.getSize().x, window.getSize().y);
- // Swap buffers
- window.display();
- }
- }
- //------------------------------------------------------------------------------
- void App::Cleanup() {
- ///painter->cleanup();
- if(window.isOpen()) window.close();
- Initialized = false;
- }
- //------------------------------------------------------------------------------
- bool App::IsRunning() {
- return Running;
- }
- //------------------------------------------------------------------------------
- bool App::IsInitialized() {
- return Initialized;
- }
- //------------------------------------------------------------------------------
- void App::SetCamera(int ox, int oy) {
- offsetX = (double)ox;
- offsetY = (double)oy;
- }
- //------------------------------------------------------------------------------
- void App::Terminate() {
- Running = false;
- }
- //------------------------------------------------------------------------------
- void App::Execute(bool edit, sf::WindowHandle context) {
- Cleanup();
- sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
- sf::VideoMode mode(480, 270, desktop.bitsPerPixel);
- // Request a 32-bits depth buffer when creating the window
- sf::ContextSettings contextSettings;
- contextSettings.depthBits = desktop.bitsPerPixel;
- contextSettings.majorVersion = 3;
- contextSettings.minorVersion = 3;
- contextSettings.antialiasingLevel = 0;
- // SFML
- if(edit) window.create(context, contextSettings);
- else window.create(mode, "Witch Hunt", sf::Style::Default, contextSettings);
- //Initialized = false;
- }
- //==============================================================================
- int App::GetWindowWidth() { return WindowWidth; }
- int App::GetWindowHeight() { return WindowHeight; }
- //==============================================================================
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement