Arturr300 Temat założony przez niniejszego użytkownika |
» 2013-05-11 15:42:16 CApp.h: #pragma once #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include <sstream> #include <iostream> #include <vector> #include <typeinfo> #include "CTools.h" #include <fstream> #define _ROZ_SZER sf::VideoMode::getDesktopMode().width #define _ROZ_WYS sf::VideoMode::getDesktopMode().height #define _FULLSCREEN 0
#define _mouse_X ((sf::Mouse::getPosition().x- (640-(_G->kamera.getCenter().x))) / SCALE) #define _mouse_Y ((sf::Mouse::getPosition().y- (512-(_G->kamera.getCenter().y))) / SCALE) using namespace std;
const float SCALE = 60.0f;
enum SHAPE { ChainShape, CircleShape, EdgeShape, PolygonShape };
struct SShape { float width, height; float radius; SHAPE shape; SShape( SHAPE shape, float width, float height ) : width( width ) , height( height ) , shape( shape ) { }; SShape( SHAPE shape, float radius ) : radius( radius ) , shape( shape ) { }; };
struct SUserData { bool naZiemi; string nazwa; SUserData( string nazwa ) : nazwa( nazwa ) { }; SUserData() : nazwa( "Default" ) { }; };
class CApp { public: sf::RenderWindow window; sf::Event zdarzenie; float fps, fps_currentTime, fps_lastTime; sf::Clock fps_clock; void Init() { window.create( sf::VideoMode::getDesktopMode(), "Craftmine" ); } void Game(); bool isPressed( Key klawisz ) { if( sf::Keyboard::isKeyPressed(( sf::Keyboard::Key ) klawisz ) ) return true; else return false; }; bool repeat; bool isReleased; bool firstrun; int i2; vector < int > v_id; vector < int > HeldKolejka; bool overlap; vector < CisHeld *> v_Held; int isHeld( Key klawisz, float czas_ms, int id ); CApp() { i2 = 0; repeat = false; firstrun = false; } virtual ~CApp() { }; static CApp * Instance() { if( !m_instance ) m_instance = new CApp; return m_instance; } private: static CApp * m_instance; };
CApp.cpp: #include "CApp.h" CApp * CApp::m_instance = 0; extern class CisHeld; void CApp::Game() { Init(); while( window.isOpen() ) { if( isHeld( T, 10, 1 ) == 1 ) { cout << "Wcisnieto T" << endl; } window.display(); window.clear( sf::Color::Blue ); } } int CApp::isHeld( Key klawisz, float czas_ms, int id ) { if(( v_id.size() > 0 && v_Held.size() > 0 || firstrun == false ) && isReleased ) { for( int i = 0; i < v_id.size(); ++i ) { if( v_id[ i ] == id ) repeat = true; } if( !repeat ) { v_Held.push_back( new CisHeld( klawisz, czas_ms, id ) ); v_id.push_back( id ); } for( int i = 0; i < v_id.size(); ++i ) { if( v_Held[ i ]->isHeld() != 0 ) { HeldKolejka.push_back( v_id[ i ] ); isReleased = false; i2 = i; } } } if( !( sf::Keyboard::isKeyPressed(( sf::Keyboard::Key ) v_Held[ i2 ]->klawisz ) ) && isReleased == false ) { isReleased = true; } for( int i = 0; i < HeldKolejka.size(); ++i ) { if( HeldKolejka[ i ] == id ) { HeldKolejka.erase( HeldKolejka.begin() + i ); return id; } } return 0; }
CTools.h: #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include <sstream> #include <iostream> #include <vector> #include <typeinfo> using namespace std;
enum Key { Unknown = - 1, A = 0, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Esc, LControl, LShift, LAlt, LSystem, RControl, RShift, RAlt, RSystem, Menu, LBracket, RBracket, SemiColon, Comma, Period, Quote, Slash, BackSlash, Tilde, Equal, Dash, Space, Return, BackSpace, Tab, PageUp, PageDown, End, Home, Insert, Delete, Add, Subtract, Multiply, Divide, Left, Right, Up, Down, Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, Pause, KeyCount };
class CisHeld { public: sf::Clock timer; bool trzymanie; Key klawisz; float czas_wms; bool czyUruchomiono; int id; CisHeld( Key klawisz2, float czas_wms2, int id2 ) { id = id2; klawisz = klawisz2; czas_wms = czas_wms2; czyUruchomiono = false; } int isHeld() { if( !czyUruchomiono ) { czyUruchomiono = true; timer.restart(); } timer.getElapsedTime().asMicroseconds(); if( timer.getElapsedTime().asMilliseconds() >= czas_wms ) { timer.restart(); if( trzymanie == true ) { czyUruchomiono = false; return id; } } if( sf::Keyboard::isKeyPressed(( sf::Keyboard::Key ) klawisz ) ) trzymanie = true; else { timer.restart(); trzymanie = false; czyUruchomiono = false; return 0; } return 0; } };
Main.cpp: #include <SFML/Graphics.hpp> #include <iostream> #include <conio.h> #include "CApp.h" using namespace std;
int main() { CApp::Instance()->Game(); return 0; }
PS. Przepraszam za bałagan w kodzie. Funkcja isHeld() była używana w moim poprzednim projekcie - działała i nadal działa bardzo dobrze |