c++ przekazanie obiektu do innego pliku
Ostatnio zmodyfikowano 2015-07-31 14:59
Jacahehe Temat założony przez niniejszego użytkownika |
c++ przekazanie obiektu do innego pliku » 2015-07-28 14:04:19 Witam! Nie mam pojecia jak przekazac obiekt ktory zrobilem w funkcji main do klasy, ktora jest w osobnym pliku. Szukalem, ale nie moge za bardzo rozwiazac tego problemu, prosze o podsuniecie jakiegos pomyslu :) add_button.hpp //tutaj potrzebny jest obiekt #ifndef add_button_hpp #define add_button_hpp #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include "game_class.hpp" #include <string> #include <sstream>
using namespace std;
class GameClass;
class CreateButton { public: sf::Texture tex_button_on; sf::Texture tex_button_off; sf::Sprite spr_button_off; sf::Text txt_button_name; bool b_button_on_off; float textsize_w; float textsize_h; short x; short y; short w; short h; void place_button( sf::String text, float where_x, float where_y, GameClass & game, int size = 40 ); void draw_and_check( GameClass & game ); }; #endif
add_button.cpp #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include "add_button.hpp" #include "game_class.hpp" #include <string> #include <sstream>
using namespace std;
void CreateButton::place_button( sf::String text, float where_x, float where_y, GameClass & game, int size = 40 ) { this->w = 310; this->h = 90; this->tex_button_on.loadFromFile( "data/graphics/Buttonon.png" ); this->tex_button_off.loadFromFile( "data/graphics/Buttonoff.png" ); this->spr_button_off.setOrigin(( w / 2 ),( h / 2 ) ); this->x = game.vm_x * where_x; this->y = game.vm_y * where_y; this->spr_button_off.setPosition( x, y ); this->txt_button_name.setString( text ); this->txt_button_name.setCharacterSize( 25 ); this->txt_button_name.setFont( game.font ); this->txt_button_name.setColor( sf::Color::White ); this->textsize_w = txt_button_name.getGlobalBounds().width; this->textsize_h = txt_button_name.getGlobalBounds().height; this->txt_button_name.setOrigin( textsize_w / 2, textsize_h ); this->txt_button_name.setPosition( x, y ); } void CreateButton::draw_and_check( GameClass & game ) { if(( x -( w / 2 ) ) <= sf::Mouse::getPosition( game.window ).x &&( x -( w / 2 ) ) + w >= sf::Mouse::getPosition( game.window ).x && ( y -( h / 2 ) ) <= sf::Mouse::getPosition( game.window ).y &&( y -( h / 2 ) ) + h >= sf::Mouse::getPosition( game.window ).y ) { b_button_on_off = true; } else { b_button_on_off = false; } if( b_button_on_off == true ) spr_button_off.setTexture( tex_button_on ); else spr_button_off.setTexture( tex_button_off ); game.window.draw( spr_button_off ); game.window.draw( txt_button_name ); }
game_class.hpp #ifndef add_button_hpp #define add_button_hpp #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include <string> #include <sstream>
using namespace std;
class GameClass { friend class CreateButton; public: unsigned short vm_x; unsigned short vm_y; unsigned short frames; sf::RenderWindow window; sf::Vector2i mouse_position; sf::Event my_event; sf::Font font; sf::Texture tex_background; bool b_main_menu_is_open; bool b_intro_is_open; bool b_options_is_open; bool b_game_menu_is_open; bool b_arena_is_open; bool b_animal_fight_is_open; }; #endif
game_class.cpp #include "game_class.hpp" #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include <string> #include <sstream>
using namespace std;
void GameClass::close_all_bools() { b_intro_is_open = false; b_options_is_open = false; b_main_menu_is_open = false; b_game_menu_is_open = false; b_arena_is_open = false; b_animal_fight_is_open = false; }
main.cpp int main() { GameClass game; game.vm_x = 1280; game.vm_y = 1024; game.frames = 30; game.language = 0; game.window.create( sf::VideoMode( game.vm_x, game.vm_y ), "Medieval", sf::Style::Fullscreen ); game.font.loadFromFile( "data/Arial.ttf" ); game.set_language(); while( game.window.isOpen() ) { while( game.window.pollEvent( game.my_event ) ) { if( game.my_event.type == sf::Event::Closed ) game.window.close(); game.window.clear(); game.close_all_bools(); game.b_main_menu_is_open = true; v_main_menu( game ); game.window.display(); } } return 0; }
|
|
pekfos |
» 2015-07-28 14:37:57 Przekaż ten obiekt jako argument konstruktora, lub metody. |
|
Jacahehe Temat założony przez niniejszego użytkownika |
» 2015-07-31 14:59:16 Wlasnie chcialem to zrobic jako argument metody, dlatego dalem 'GameClass & game' w argumentach, ale chyba nie o to chodzi ;) Poszukam jeszcze bo nie wiem jak to zrobic |
|
« 1 » |