Tacola Temat założony przez niniejszego użytkownika |
[C++ obiektowo][SFML 1.6][SFML 2.5] Przetłumaczenie kodu na najnowszy język sfmla. » 2018-06-08 18:54:14 Napisałam grę 4 w rzędzie na sfml-u 1.6 i potrzebuję wsparcia przy zmianie na sfml 2.5 - nie mam kompletnie pojęcia jak się za to zabrać. Napisałam ją już dosyć dawno i nie jestem na bieżąco z sfmlem :< Poniżej podaje swój kod: Main.cpp #include "Plansza.h" #include "Runda.h"
#include <iostream> #include <string>
#include<SFML\Graphics.hpp> #include<SFML\Window.hpp> #include<SFML\System.hpp>
using namespace std;
int main() { unsigned int antialias = 8; sf::RenderWindow Gra( sf::VideoMode::GetMode( 0 ), "4 w Rzędzie", sf::Style::Resize | sf::Style::Close, sf::WindowSettings::WindowSettings( 24, 8, antialias ) ); Gra.SetFramerateLimit( 30 ); PLANSZA plansza( Gra ); RUNDA runda( plansza ); while( Gra.IsOpened() ) { sf::Event wydarzenie; while( Gra.GetEvent( wydarzenie ) ) { if( wydarzenie.Type == sf::Event::Closed ) Gra.Close(); else if( wydarzenie.Type == wydarzenie.MouseButtonReleased && wydarzenie.MouseButton.Button == sf::Mouse::Left ) runda.wloz_do_kolumny( Gra.GetInput() ); } Gra.Clear( plansza.Kolor_planszy ); plansza.rysowanie(); Gra.Display(); } return EXIT_SUCCESS; }
Plansza.h #ifndef PLANSZA_H_ #define PLANSZA_H_
#include<SFML\Graphics.hpp> #include<SFML\Audio.hpp>
#include <iostream>
#define Wiersze 6 #define Kolumny 7
enum Zeton { Pusty, Czerwony, Niebieski, };
enum Kontur { Normalny, Podkreslony };
enum Ruch { Polozenie, Pelny, KoniecGry };
class PLANSZA { public: PLANSZA( sf::RenderWindow & App ); PLANSZA(); ~PLANSZA(); void restart(); void rysowanie(); Ruch dodaj_do_kolumny( unsigned int k, Zeton kolorGracza ); int rozdz_okna(); sf::Color Kolor_planszy; private: bool jestKoniecGry( const unsigned int y, const unsigned int x ); bool SprawdzPoziom( const unsigned int y, const unsigned int x ); bool SprawdzPion( const unsigned int y, const unsigned int x ); bool SprawdzUkos( const unsigned int y, const unsigned int x ); Zeton plansza[ Wiersze + 2 ][ Kolumny + 2 ]; Kontur kontur_Planszy[ Wiersze + 2 ][ Kolumny + 2 ]; sf::RenderWindow & okno; sf::Color CzerwonyKolor; sf::Color NiebieskiKolor; sf::Color PustyKolor; sf::Color LiniaKolor; sf::Color PodkreslonyKolor; float wys_okna; float szer_okna; float kontur; float zasieg; float przestrzen_w, przestrzen_k; }; #endif
Plansza.cpp #include "Plansza.h" #include "Runda.h"
#include <string> #include<iostream> using namespace std;
PLANSZA::PLANSZA( sf::RenderWindow & App ) : okno( App ) { Kolor_planszy = sf::Color::Color( 255, 255, 157 ); CzerwonyKolor = sf::Color::Color( 214, 71, 0 ); NiebieskiKolor = sf::Color::Color( 23, 125, 208 ); PustyKolor = sf::Color::Color( 224, 224, 224 ); LiniaKolor = sf::Color::Color( 232, 232, 0 ); PodkreslonyKolor = sf::Color::Color( 19, 240, 57 ); wys_okna = okno.GetHeight(); szer_okna = okno.GetWidth(); if( wys_okna < szer_okna ) { zasieg = wys_okna /( Wiersze * 2.0 ); przestrzen_w = 1.0 / 2.0 * zasieg; kontur = 1.0 / 15.0 * zasieg; zasieg = zasieg - przestrzen_w / 2.0 - kontur; przestrzen_k =( szer_okna -( Kolumny * 2.0 ) * zasieg ) /( Kolumny + 1.0 ); } else { zasieg = szer_okna /( Kolumny * 2.0 ); przestrzen_k = 1.0 / 2.0 * zasieg; kontur = 1.0 / 15.0 * zasieg; zasieg = zasieg - przestrzen_k / 2.0 - kontur; przestrzen_k =( szer_okna -( Wiersze * 2.0 ) * zasieg ) /( Wiersze + 1.0 ); } restart(); }
PLANSZA::~PLANSZA() { }
void PLANSZA::restart() { for( int y = 0; y < Wiersze + 1; y++ ) { for( int x = 0; x < Kolumny + 1; x++ ) { plansza[ y ][ x ] = Pusty; kontur_Planszy[ y ][ x ] = Normalny; } } }
void PLANSZA::rysowanie() { for( int y = 1; y <= Wiersze; y++ ) { float srodek_wierszy =( przestrzen_w + zasieg ) * y + zasieg *( y - 1 ); for( int x = 1; x <= Kolumny; x++ ) { float srodek_kolumn =( przestrzen_k + zasieg ) * x + zasieg *( x - 1 ); sf::Color wypelniacz; if( plansza[ y ][ x ] == Pusty ) wypelniacz = PustyKolor; else if( plansza[ y ][ x ] == Czerwony ) wypelniacz = CzerwonyKolor; else wypelniacz = NiebieskiKolor; sf::Color Kolor_krawedzie; if( kontur_Planszy[ y ][ x ] == Podkreslony ) Kolor_krawedzie = PodkreslonyKolor; else Kolor_krawedzie = LiniaKolor; okno.Draw( sf::Shape::Circle( przestrzen_w, przestrzen_k, zasieg, wypelniacz, kontur, Kolor_krawedzie ) ); } } }
Ruch PLANSZA::dodaj_do_kolumny( unsigned int kolumna, Zeton kolorGracza ) { for( int i = Wiersze; i > 0; i-- ) { if( plansza[ i ][ kolumna ] == Pusty ) { plansza[ i ][ kolumna ] = kolorGracza; if( jestKoniecGry( i, kolumna ) ) return KoniecGry; else return Polozenie; } } return Pelny; }
bool PLANSZA::jestKoniecGry( const unsigned int y, const unsigned int x ) { bool t = false; t |= SprawdzPoziom( y, x ); t |= SprawdzPion( y, x ); t |= SprawdzUkos( y, x ); return t; }
bool PLANSZA::SprawdzPoziom( const unsigned int y, const unsigned int x ) { Zeton kolor_Zetonu = plansza[ y ][ x ]; int l = 1, i; for( i = x + 1; plansza[ y ][ i ] == kolor_Zetonu; i++ ) l++; for( i = x - 1; plansza[ y ][ i ] == kolor_Zetonu; i-- ) l++; if( l >= 4 ) { while( plansza[ y ][ ++i ] == kolor_Zetonu ) kontur_Planszy[ y ][ i ] = Podkreslony; return true; } else return false; }
bool PLANSZA::SprawdzPion( const unsigned int y, const unsigned int x ) { Zeton kolor_Zetonu = plansza[ y ][ x ]; int l = 1, i; for( i = y + 1; plansza[ i ][ x ] == kolor_Zetonu; i++ ) l++; for( i = y - 1; plansza[ i ][ x ] == kolor_Zetonu; i-- ) l++; if( l >= 4 ) { while( plansza[ ++i ][ x ] == kolor_Zetonu ) kontur_Planszy[ i ][ x ] = Podkreslony; return true; } else return false; } bool PLANSZA::SprawdzUkos( const unsigned int y, const unsigned int x ) { Zeton kolor_Zetonu = plansza[ y ][ x ]; bool t = false; int cnt = 1, i, j; for( i = y + 1, j = x - 1; plansza[ i ][ j ] == kolor_Zetonu; i++, j-- ) cnt++; for( i = y - 1, j = x + 1; plansza[ i ][ j ] == kolor_Zetonu; i--, j++ ) cnt++; if( cnt >= 4 ) { while( plansza[ ++i ][ --j ] == kolor_Zetonu ) kontur_Planszy[ i ][ j ] = Podkreslony; t = true; } cnt = 1; for( i = y + 1, j = x + 1; plansza[ i ][ j ] == kolor_Zetonu; i++, j++ ) cnt++; for( i = y - 1, j = x - 1; plansza[ i ][ j ] == kolor_Zetonu; i--, j-- ) cnt++; if( cnt >= 4 ) { while( plansza[ ++i ][ ++j ] == kolor_Zetonu ) kontur_Planszy[ i ][ j ] = Podkreslony; return true; } else return t; }
int PLANSZA::rozdz_okna() { return okno.GetWidth(); }
Runda.h #ifndef RUNDA_H_ #define RUNDA_H_
#include"Plansza.h"
class PLANSZA;
class RUNDA : public PLANSZA { public: RUNDA( PLANSZA & Refer ); ~RUNDA(); void ustalenieTury(); int nrTury(); RUNDA operator +( const RUNDA & other ); RUNDA operator =( const RUNDA & other ); void wloz_do_kolumny( const sf::Input & klik ); private: unsigned int kolumna; PLANSZA & plansza; int Tury; Zeton nast_Gracz; Ruch stanGry; };
#endif
Runda.cpp #include "Runda.h" #include "Plansza.h"
RUNDA::RUNDA( PLANSZA & Refer ) : plansza( Refer ) { Tury = 0; nast_Gracz = Czerwony; }
RUNDA::~RUNDA() { }
void RUNDA::ustalenieTury() { Tury = Tury + 1; }
int RUNDA::nrTury() { return Tury; } RUNDA RUNDA::operator +( const RUNDA & other ) { this->Tury = Tury + other.Tury; } RUNDA RUNDA::operator =( const RUNDA & other ) { this->Tury = other.Tury; }
void RUNDA::wloz_do_kolumny( const sf::Input & klik ) { kolumna = float( klik.GetMouseX() ) / plansza.rozdz_okna() * Kolumny + 1; if( stanGry == KoniecGry ) { plansza.restart(); Tury = 0; stanGry = Pelny; } else { stanGry = plansza.dodaj_do_kolumny( kolumna, nast_Gracz ); if( stanGry != Pelny ) { Tury++; if( nast_Gracz == Czerwony ) nast_Gracz = Niebieski; else nast_Gracz = Czerwony; if( Tury == Kolumny * Wiersze ) stanGry = KoniecGry; } } }
Dzięki z góry za pomoc :) |
|
pekfos |
» 2018-06-08 19:26:57 Zacznij od tego, że metody SFML 2.x zaczynają się z małej litery. |
|
Tacola Temat założony przez niniejszego użytkownika |
» 2018-06-08 21:12:47 Okej, zrobiłam tak - tylko pytanie: Są fragmenty kodu, które mają inną składnie niż Sfml 2.x np. W main else if( wydarzenie.Type == wydarzenie.MouseButtonReleased && wydarzenie.MouseButton.Button == sf::Mouse::Left ) runda.wloz_do_kolumny( Gra.GetInput() );
W plansza cpp okno.Draw( sf::Shape::Circle( przestrzen_w, przestrzen_k, zasieg, wypelniacz, kontur, Kolor_krawedzie ) ); . . . int PLANSZA::rozdz_okna() { return okno.GetWidth(); }
Oraz w Rundzie void wloz_do_kolumny( const sf::Input & klik ); . . . kolumna = float( klik.GetMouseX() ) / plansza.rozdz_okna() * Kolumny + 1;
Jakieś porady, by nie zepsuć połowy kodu? :) |
|
pekfos |
» 2018-06-08 21:18:47 |
|
« 1 » |