diff --git a/CMakeLists.txt b/CMakeLists.txt index b4b94df..5861149 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,16 @@ cmake_minimum_required(VERSION 2.8) - set(EXE_NAME tetris) project(${EXE_NAME}) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + + +find_package(SDL2 REQUIRED) +find_package(SDL2_ttf REQUIRED) +include_directories(${SDL2_INCLUDE_DIR} ${SDL2TTF_INCLUDE_DIRS}) +set(TETRIS_LIBRARIES ${SDL2_LIBRARY} ${SDL2TTF_LIBRARY} ) + add_subdirectory(src) diff --git a/README.md b/README.md index e69de29..4c1428a 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,6 @@ +# Tetris +Basic tetris write in C++ and SDL + +## Changelog +* 4/6/2015 + * Replaced SDL with SDL2 \ No newline at end of file diff --git a/cmake/FindSDL2.cmake b/cmake/FindSDL2.cmake new file mode 100644 index 0000000..071287b --- /dev/null +++ b/cmake/FindSDL2.cmake @@ -0,0 +1,171 @@ +# Locate SDL2 library +# This module defines +# SDL2_LIBRARY, the name of the library to link against +# SDL2_FOUND, if false, do not try to link to SDL2 +# SDL2_INCLUDE_DIR, where to find SDL.h +# +# This module responds to the the flag: +# SDL2_BUILDING_LIBRARY +# If this is defined, then no SDL2main will be linked in because +# only applications need main(). +# Otherwise, it is assumed you are building an application and this +# module will attempt to locate and set the the proper link flags +# as part of the returned SDL2_LIBRARY variable. +# +# Don't forget to include SDLmain.h and SDLmain.m your project for the +# OS X framework based version. (Other versions link to -lSDL2main which +# this module will try to find on your behalf.) Also for OS X, this +# module will automatically add the -framework Cocoa on your behalf. +# +# +# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration +# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library +# (SDL2.dll, libsdl2.so, SDL2.framework, etc). +# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again. +# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value +# as appropriate. These values are used to generate the final SDL2_LIBRARY +# variable, but when these values are unset, SDL2_LIBRARY does not get created. +# +# +# $SDL2DIR is an environment variable that would +# correspond to the ./configure --prefix=$SDL2DIR +# used in building SDL2. +# l.e.galup 9-20-02 +# +# Modified by Eric Wing. +# Added code to assist with automated building by using environmental variables +# and providing a more controlled/consistent search behavior. +# Added new modifications to recognize OS X frameworks and +# additional Unix paths (FreeBSD, etc). +# Also corrected the header search path to follow "proper" SDL guidelines. +# Added a search for SDL2main which is needed by some platforms. +# Added a search for threads which is needed by some platforms. +# Added needed compile switches for MinGW. +# +# On OSX, this will prefer the Framework version (if found) over others. +# People will have to manually change the cache values of +# SDL2_LIBRARY to override this selection or set the CMake environment +# CMAKE_INCLUDE_PATH to modify the search paths. +# +# Note that the header path has changed from SDL2/SDL.h to just SDL.h +# This needed to change because "proper" SDL convention +# is #include "SDL.h", not . This is done for portability +# reasons because not all systems place things in SDL2/ (see FreeBSD). + +#============================================================================= +# Copyright 2003-2009 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(SDL2_ARCH_64 TRUE) + set(SDL2_PROCESSOR_ARCH "x64") +else() + set(SDL2_ARCH_64 FALSE) + set(SDL2_PROCESSOR_ARCH "x86") +endif(CMAKE_SIZEOF_VOID_P EQUAL 8) + +SET(SDL2_SEARCH_PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt +) + +FIND_PATH(SDL2_INCLUDE_DIR SDL.h + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES include/SDL2 include SDL2 + PATHS ${SDL2_SEARCH_PATHS} +) + +FIND_LIBRARY(SDL2_LIBRARY_TEMP + NAMES SDL2 + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES lib64 lib lib/${SDL2_PROCESSOR_ARCH} + PATHS ${SDL2_SEARCH_PATHS} +) + +IF(NOT SDL2_BUILDING_LIBRARY) + IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") + # Non-OS X framework versions expect you to also dynamically link to + # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms + # seem to provide SDL2main for compatibility even though they don't + # necessarily need it. + FIND_LIBRARY(SDL2MAIN_LIBRARY + NAMES SDL2main + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES lib64 lib lib/${SDL2_PROCESSOR_ARCH} + PATHS ${SDL2_SEARCH_PATHS} + ) + ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") +ENDIF(NOT SDL2_BUILDING_LIBRARY) + +# SDL2 may require threads on your system. +# The Apple build may not need an explicit flag because one of the +# frameworks may already provide it. +# But for non-OSX systems, I will use the CMake Threads package. +IF(NOT APPLE) + FIND_PACKAGE(Threads) +ENDIF(NOT APPLE) + +# MinGW needs an additional library, mwindows +# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows +# (Actually on second look, I think it only needs one of the m* libraries.) +IF(MINGW) + SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW") +ENDIF(MINGW) + +IF(SDL2_LIBRARY_TEMP) + # For SDL2main + IF(NOT SDL2_BUILDING_LIBRARY) + IF(SDL2MAIN_LIBRARY) + SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP}) + ENDIF(SDL2MAIN_LIBRARY) + ENDIF(NOT SDL2_BUILDING_LIBRARY) + + # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa. + # CMake doesn't display the -framework Cocoa string in the UI even + # though it actually is there if I modify a pre-used variable. + # I think it has something to do with the CACHE STRING. + # So I use a temporary variable until the end so I can set the + # "real" variable in one-shot. + IF(APPLE) + SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa") + ENDIF(APPLE) + + # For threads, as mentioned Apple doesn't need this. + # In fact, there seems to be a problem if I used the Threads package + # and try using this line, so I'm just skipping it entirely for OS X. + IF(NOT APPLE) + SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) + ENDIF(NOT APPLE) + + # For MinGW library + IF(MINGW) + SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP}) + ENDIF(MINGW) + + # Set the final string here so the GUI reflects the final state. + SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found") + # Set the temp variable to INTERNAL so it is not seen in the CMake GUI + SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "") +ENDIF(SDL2_LIBRARY_TEMP) + +INCLUDE(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR) \ No newline at end of file diff --git a/cmake/FindSDL2_ttf.cmake b/cmake/FindSDL2_ttf.cmake new file mode 100644 index 0000000..e715f36 --- /dev/null +++ b/cmake/FindSDL2_ttf.cmake @@ -0,0 +1,163 @@ +# Locate SDL2 library +# This module defines +# SDL2_LIBRARY, the name of the library to link against +# SDL2_FOUND, if false, do not try to link to SDL2 +# SDL2_INCLUDE_DIR, where to find SDL.h +# +# This module responds to the the flag: +# SDL2_BUILDING_LIBRARY +# If this is defined, then no SDL2main will be linked in because +# only applications need main(). +# Otherwise, it is assumed you are building an application and this +# module will attempt to locate and set the the proper link flags +# as part of the returned SDL2_LIBRARY variable. +# +# Don't forget to include SDLmain.h and SDLmain.m your project for the +# OS X framework based version. (Other versions link to -lSDL2main which +# this module will try to find on your behalf.) Also for OS X, this +# module will automatically add the -framework Cocoa on your behalf. +# +# +# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration +# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library +# (SDL2.dll, libsdl2.so, SDL2.framework, etc). +# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again. +# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value +# as appropriate. These values are used to generate the final SDL2_LIBRARY +# variable, but when these values are unset, SDL2_LIBRARY does not get created. +# +# +# $SDL2DIR is an environment variable that would +# correspond to the ./configure --prefix=$SDL2DIR +# used in building SDL2. +# l.e.galup 9-20-02 +# +# Modified by Eric Wing. +# Added code to assist with automated building by using environmental variables +# and providing a more controlled/consistent search behavior. +# Added new modifications to recognize OS X frameworks and +# additional Unix paths (FreeBSD, etc). +# Also corrected the header search path to follow "proper" SDL guidelines. +# Added a search for SDL2main which is needed by some platforms. +# Added a search for threads which is needed by some platforms. +# Added needed compile switches for MinGW. +# +# On OSX, this will prefer the Framework version (if found) over others. +# People will have to manually change the cache values of +# SDL2_LIBRARY to override this selection or set the CMake environment +# CMAKE_INCLUDE_PATH to modify the search paths. +# +# Note that the header path has changed from SDL2/SDL.h to just SDL.h +# This needed to change because "proper" SDL convention +# is #include "SDL.h", not . This is done for portability +# reasons because not all systems place things in SDL2/ (see FreeBSD). + +#============================================================================= +# Copyright 2003-2009 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +SET(SDL2TTF_SEARCH_PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt +) + +FIND_PATH(SDL2TTF_INCLUDE_DIR SDL_ttf.h + HINTS + $ENV{SDL2TTFDIR} + PATH_SUFFIXES include/SDL2 include + PATHS ${SDL2TTF_SEARCH_PATHS} +) + +FIND_LIBRARY(SDL2TTF_LIBRARY_TEMP + NAMES SDL2_ttf + HINTS + $ENV{SDL2TTFDIR} + PATH_SUFFIXES lib64 lib + PATHS ${SDL2TTF_SEARCH_PATHS} +) + +IF(NOT SDL2TTF_BUILDING_LIBRARY) + IF(NOT ${SDL2TTF_INCLUDE_DIR} MATCHES ".framework") + # Non-OS X framework versions expect you to also dynamically link to + # SDL2TTFmain. This is mainly for Windows and OS X. Other (Unix) platforms + # seem to provide SDL2TTFmain for compatibility even though they don't + # necessarily need it. + FIND_LIBRARY(SDL2TTFMAIN_LIBRARY + NAMES SDL2_ttf + HINTS + $ENV{SDL2TTFDIR} + PATH_SUFFIXES lib64 lib + PATHS ${SDL2TTF_SEARCH_PATHS} + ) + ENDIF(NOT ${SDL2TTF_INCLUDE_DIR} MATCHES ".framework") +ENDIF(NOT SDL2TTF_BUILDING_LIBRARY) + +# SDL2TTF may require threads on your system. +# The Apple build may not need an explicit flag because one of the +# frameworks may already provide it. +# But for non-OSX systems, I will use the CMake Threads package. +IF(NOT APPLE) + FIND_PACKAGE(Threads) +ENDIF(NOT APPLE) + +# MinGW needs an additional library, mwindows +# It's total link flags should look like -lmingw32 -lSDL2TTFmain -lSDL2TTF -lmwindows +# (Actually on second look, I think it only needs one of the m* libraries.) +IF(MINGW) + SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW") +ENDIF(MINGW) + +IF(SDL2TTF_LIBRARY_TEMP) + # For SDL2TTFmain + IF(NOT SDL2TTF_BUILDING_LIBRARY) + IF(SDL2TTFMAIN_LIBRARY) + SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTFMAIN_LIBRARY} ${SDL2TTF_LIBRARY_TEMP}) + ENDIF(SDL2TTFMAIN_LIBRARY) + ENDIF(NOT SDL2TTF_BUILDING_LIBRARY) + + # For OS X, SDL2TTF uses Cocoa as a backend so it must link to Cocoa. + # CMake doesn't display the -framework Cocoa string in the UI even + # though it actually is there if I modify a pre-used variable. + # I think it has something to do with the CACHE STRING. + # So I use a temporary variable until the end so I can set the + # "real" variable in one-shot. + IF(APPLE) + SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTF_LIBRARY_TEMP} "-framework Cocoa") + ENDIF(APPLE) + + # For threads, as mentioned Apple doesn't need this. + # In fact, there seems to be a problem if I used the Threads package + # and try using this line, so I'm just skipping it entirely for OS X. + IF(NOT APPLE) + SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTF_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) + ENDIF(NOT APPLE) + + # For MinGW library + IF(MINGW) + SET(SDL2TTF_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2TTF_LIBRARY_TEMP}) + ENDIF(MINGW) + + # Set the final string here so the GUI reflects the final state. + SET(SDL2TTF_LIBRARY ${SDL2TTF_LIBRARY_TEMP} CACHE STRING "Where the SDL2TTF Library can be found") + # Set the temp variable to INTERNAL so it is not seen in the CMake GUI + SET(SDL2TTF_LIBRARY_TEMP "${SDL2TTF_LIBRARY_TEMP}" CACHE INTERNAL "") +ENDIF(SDL2TTF_LIBRARY_TEMP) + +INCLUDE(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_ttf REQUIRED_VARS SDL2TTF_LIBRARY SDL2TTF_INCLUDE_DIR) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fa0007..1d9d231 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,23 +1,24 @@ cmake_minimum_required(VERSION 2.8) -add_executable(${EXE_NAME} - scoreitem.cpp - scorestate.cpp - stringinput.cpp - part.cpp - main.cpp - global.cpp - map.cpp - resource.cpp - game.cpp - pausestate.cpp - playstate.cpp - menustate.cpp - menuitem.cpp - menu.cpp +add_executable( + ${EXE_NAME} + scoreitem.cpp + scorestate.cpp + stringinput.cpp + part.cpp + main.cpp + global.cpp + map.cpp + resource.cpp + game.cpp + pausestate.cpp + playstate.cpp + menustate.cpp + menuitem.cpp + menu.cpp ) -target_link_libraries(${EXE_NAME} SDL SDL_ttf) +target_link_libraries(${EXE_NAME} ${TETRIS_LIBRARIES}) set(CMAKE_CXX_FLAGS "-Wextra -ftabstop=4 -march=native -std=gnu++11 -fshow-column -ftabstop=4 -frounding-math -pipe") diff --git a/src/game.cpp b/src/game.cpp index 8fcf29d..652934e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -21,21 +21,22 @@ bool game::Init() return false; } - SDL_EnableUNICODE ( SDL_ENABLE ); + gWindow = SDL_CreateWindow( "[SG]Tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SIZE + 130, HEIGHT * SIZE, SDL_WINDOW_SHOWN ); - SDL_WM_SetCaption ( "[SG]Tetris", NULL ); - - m_pScreen = SDL_SetVideoMode ( WIDTH * SIZE + 130, HEIGHT * SIZE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF ); - - if ( m_pScreen == NULL ) + if ( gWindow == NULL ) { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); cerr << "[ERROR] Cannot setup screen" << endl; m_bRunning = false; return false; } + else + { + gScreenSurface = SDL_GetWindowSurface( gWindow ); + } if ( TTF_Init() ) @@ -48,9 +49,9 @@ bool game::Init() } - m_bRunning = Load(); + m_bRunning = Load(); m_bGameOver = false; - tick = 0; + tick = 0; return m_bRunning; } @@ -155,10 +156,14 @@ void game::Draw() states.back()->Draw ( this ); } +SDL_Window * game::GetWindow() +{ + return gWindow; +} SDL_Surface * game::GetScreen() { - return m_pScreen; + return gScreenSurface; } bool game::Running() @@ -172,9 +177,11 @@ void game::Clean() cout << "Cleanning..." << endl; # endif - TTF_CloseFont ( fontMenu ); - - TTF_Quit(); + SDL_DestroyWindow( gWindow ); + gWindow = NULL; + TTF_CloseFont ( fontMenu ); + + TTF_Quit(); SDL_Quit(); } @@ -226,3 +233,8 @@ TTF_Font * game::GetfontMenu() { return fontMenu; } + +void game::SetRunning ( bool Running ) +{ + m_bRunning = Running; +} diff --git a/src/game.hpp b/src/game.hpp index 105b1ca..f580919 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -3,11 +3,12 @@ #include #include -#include -#include +#include +#include #include "map.hpp" #include "part.hpp" +#include "resource.hpp" using namespace std; @@ -40,6 +41,7 @@ class game SDL_Surface * GetScreen(); Uint32 GetStart(); + SDL_Window * GetWindow(); TTF_Font * GetfontMenu(); TTF_Font * GetfontGame(); @@ -56,7 +58,8 @@ class game void UpdateFPS(); private: - SDL_Surface * m_pScreen; + SDL_Window * gWindow; + SDL_Surface * gScreenSurface; vector states; bool m_bRunning; diff --git a/src/global.hpp b/src/global.hpp index f94e847..919a2fd 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -1,8 +1,8 @@ #ifndef GLOBAL_HPP_INCLUDED #define GLOBAL_HPP_INCLUDED -#include -#include +#include +#include #include #include diff --git a/src/main.cpp b/src/main.cpp index 6d9bcb3..42f9b40 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include "resource.hpp" #include "global.hpp" @@ -24,7 +24,6 @@ int main ( int argc, char ** argv ) return 0; } - Game.ChangeState ( MenuState::Instance() ); while ( Game.Running() ) diff --git a/src/menuitem.hpp b/src/menuitem.hpp index a703514..1a8d99f 100644 --- a/src/menuitem.hpp +++ b/src/menuitem.hpp @@ -3,8 +3,8 @@ #include -#include -#include +#include +#include #include "global.hpp" #include "game.hpp" diff --git a/src/menustate.cpp b/src/menustate.cpp index 6000851..11c3c68 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -96,7 +96,8 @@ void MenuState::Draw(game* Game) { menu->Draw(Game); - SDL_Flip(Game->GetScreen()); + SDL_UpdateWindowSurface( Game->GetWindow() ); + //SDL_Flip(Game->GetScreen()); } diff --git a/src/menustate.hpp b/src/menustate.hpp index a8d32df..b495d9a 100644 --- a/src/menustate.hpp +++ b/src/menustate.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include "game.hpp" #include "gamestate.hpp" diff --git a/src/part.hpp b/src/part.hpp index f22dd20..c97e3e0 100644 --- a/src/part.hpp +++ b/src/part.hpp @@ -2,7 +2,7 @@ #define PART_HPP_INCLUDED #include -#include +#include #include "resource.hpp" #include "global.hpp" diff --git a/src/pausestate.cpp b/src/pausestate.cpp index 989a1c0..0c981cc 100644 --- a/src/pausestate.cpp +++ b/src/pausestate.cpp @@ -89,7 +89,8 @@ void PauseState::Draw(game* Game) { menu->Draw(Game); - SDL_Flip(Game->GetScreen()); + //SDL_Flip(Game->GetScreen()); + SDL_UpdateWindowSurface( Game->GetWindow() ); } diff --git a/src/pausestate.hpp b/src/pausestate.hpp index d4baa03..0fe0b81 100644 --- a/src/pausestate.hpp +++ b/src/pausestate.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include "game.hpp" #include "gamestate.hpp" diff --git a/src/playstate.cpp b/src/playstate.cpp index 8617532..61cadf8 100644 --- a/src/playstate.cpp +++ b/src/playstate.cpp @@ -23,7 +23,7 @@ void PlayState::Init() textColor = make_color(255, 255, 255); - SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); +// SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); # ifdef DEBUG std::cout << "PlayState Init Successful" << std::endl; @@ -32,7 +32,7 @@ void PlayState::Init() void PlayState::Clean() { - SDL_EnableKeyRepeat ( 0, 0 ); +// SDL_EnableKeyRepeat ( 0, 0 ); # ifdef DEBUG std::cout << "PlayState Clean Successful" << std::endl; @@ -41,7 +41,7 @@ void PlayState::Clean() void PlayState::Pause() { - SDL_EnableKeyRepeat ( 0, 0 ); +// SDL_EnableKeyRepeat ( 0, 0 ); # ifdef DEBUG std::cout << "PlayState Paused" << std::endl; @@ -50,7 +50,7 @@ void PlayState::Pause() void PlayState::Resume() { - SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); +// SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); # ifdef DEBUG std::cout << "PlayState Resumed" << std::endl; @@ -226,9 +226,11 @@ void PlayState::Update ( game* Game ) { /* Start enter name */ nameEntering = true; + SDL_StartTextInput(); if (nameEntered) { + SDL_StopTextInput(); nameEntering = false; nameEntered = false; //text.setString("Score " + intToStr(Map.getScore()) + "\n" + "Press enter"); @@ -321,7 +323,8 @@ void PlayState::Draw ( game* Game ) name.show_centered(Game); } - SDL_Flip ( Game->GetScreen() ); + SDL_UpdateWindowSurface( Game->GetWindow() ); + //SDL_Flip ( Game->GetScreen() ); } diff --git a/src/playstate.hpp b/src/playstate.hpp index f13616d..2e11540 100644 --- a/src/playstate.hpp +++ b/src/playstate.hpp @@ -1,7 +1,7 @@ # ifndef PLAY_STATE_HPP_INCLUDED # define PLAY_STATE_HPP_INCLUDED -# include +# include # include # include # include diff --git a/src/scoreitem.hpp b/src/scoreitem.hpp index 62d136f..ea68631 100644 --- a/src/scoreitem.hpp +++ b/src/scoreitem.hpp @@ -2,8 +2,8 @@ #define SCOREITEM_H # include -# include -# include +# include +# include # include "game.hpp" # include "global.hpp" diff --git a/src/scorestate.cpp b/src/scorestate.cpp index 1204343..f306cfb 100644 --- a/src/scorestate.cpp +++ b/src/scorestate.cpp @@ -16,7 +16,8 @@ void ScoreState::Draw ( game * Game ) Items[i].Draw(Game, i * 35); } - SDL_Flip(Game->GetScreen()); + //SDL_Flip(Game->GetScreen()); + SDL_UpdateWindowSurface( Game->GetWindow() ); } void ScoreState::Update ( game * Game ) diff --git a/src/states.hpp b/src/states.hpp index b2201ba..b9a0435 100644 --- a/src/states.hpp +++ b/src/states.hpp @@ -1,4 +1,4 @@ -# include "playstate.hpp" -# include "menustate.hpp" -# include "pausestate.hpp" -# include "scorestate.hpp" +#include "playstate.hpp" +#include "menustate.hpp" +#include "pausestate.hpp" +#include "scorestate.hpp" diff --git a/src/stringinput.cpp b/src/stringinput.cpp index dd1db89..fab8b8d 100644 --- a/src/stringinput.cpp +++ b/src/stringinput.cpp @@ -15,62 +15,71 @@ StringInput::~StringInput() void StringInput::handle_input ( game * Game, SDL_Event *event) { + string temp; + //If a key was pressed if ( event->type == SDL_KEYDOWN ) { //Keep a copy of the current version of the string - string temp = str; + temp = str; //If the string less than maximum size if ( str.length() <= 11 ) { - //If the key is a number - if ( ( event->key.keysym.unicode >= ( Uint16 ) '0' ) && ( event->key.keysym.unicode <= ( Uint16 ) '9' ) ) + //Special key input + if( event->type == SDL_KEYDOWN ) { - //Append the character - str += ( char ) event->key.keysym.unicode; - } - else if ( ( event->key.keysym.unicode == ( Uint16 ) '-' ) ) - { - //Append the character - str += ( char ) event->key.keysym.unicode; - } - //If the key is a uppercase letter - else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'A' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'Z' ) ) - { - //Append the character - str += ( char ) event->key.keysym.unicode; - } - //If the key is a lowercase letter - else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'a' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'z' ) ) - { - //Append the character - str += ( char ) event->key.keysym.unicode; + //Handle backspace + if( event->key.keysym.sym == SDLK_BACKSPACE && str.length() > 0 ) + { + //lop off character + str.pop_back(); + } + //Handle copy + else if( event->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL ) + { + SDL_SetClipboardText( str.c_str() ); + } + //Handle paste + else if( event->key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL ) + { + str = SDL_GetClipboardText(); + } } + } + } + //Special text input event + else if( event->type == SDL_TEXTINPUT ) + { + //Keep a copy of the current version of the string + temp = str; - #ifdef DEBUG - cout << str << endl; - #endif - - //If backspace was pressed and the string isn't blank - if ( ( event->key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) ) + //If the string less than maximum size + if ( str.length() <= 11 ) { - //Remove a character from the end - str.erase ( str.length() - 1 ); - } - - //If the string was changed - if ( str != temp ) - { - //Free the old surface - SDL_FreeSurface ( text ); - - //Render a new text surface - text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor ); + //Not copy or pasting + if( !( ( event->text.text[ 0 ] == 'c' || event->text.text[ 0 ] == 'C' ) && ( event->text.text[ 0 ] == 'v' || event->text.text[ 0 ] == 'V' ) && SDL_GetModState() & KMOD_CTRL ) ) + { + //Append character + str += event->text.text; + } } } + #ifdef DEBUG + cout << str << endl; + #endif + + //If the string was changed + if ( str != temp ) + { + //Free the old surface + SDL_FreeSurface ( text ); + + //Render a new text surface + text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor ); + } } void StringInput::show_centered ( game * Game ) diff --git a/src/stringinput.hpp b/src/stringinput.hpp index f02d0d9..e8e6fbc 100644 --- a/src/stringinput.hpp +++ b/src/stringinput.hpp @@ -3,8 +3,8 @@ # include # include -# include -# include +# include +# include # include "game.hpp" # include "global.hpp"