Replaced SDL 1.2 with SDL 2.0

This commit is contained in:
SGOrava 2015-04-06 21:25:23 +02:00
parent 3d1eb7b9b4
commit 71f647043a
22 changed files with 475 additions and 98 deletions

View File

@ -1,9 +1,16 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
set(EXE_NAME tetris) set(EXE_NAME tetris)
project(${EXE_NAME}) 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) add_subdirectory(src)

View File

@ -0,0 +1,6 @@
# Tetris
Basic tetris write in C++ and SDL
## Changelog
* 4/6/2015
* Replaced SDL with SDL2

171
cmake/FindSDL2.cmake Normal file
View File

@ -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 <SDL2/SDL.h>. 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)

163
cmake/FindSDL2_ttf.cmake Normal file
View File

@ -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 <SDL2/SDL.h>. 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)

View File

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
add_executable(${EXE_NAME} add_executable(
${EXE_NAME}
scoreitem.cpp scoreitem.cpp
scorestate.cpp scorestate.cpp
stringinput.cpp stringinput.cpp
@ -17,7 +18,7 @@ add_executable(${EXE_NAME}
menu.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") set(CMAKE_CXX_FLAGS "-Wextra -ftabstop=4 -march=native -std=gnu++11 -fshow-column -ftabstop=4 -frounding-math -pipe")

View File

@ -21,21 +21,22 @@ bool game::Init()
return false; 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 ); if ( gWindow == NULL )
m_pScreen = SDL_SetVideoMode ( WIDTH * SIZE + 130, HEIGHT * SIZE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
if ( m_pScreen == NULL )
{ {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
cerr << "[ERROR] Cannot setup screen" << endl; cerr << "[ERROR] Cannot setup screen" << endl;
m_bRunning = false; m_bRunning = false;
return false; return false;
} }
else
{
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
if ( TTF_Init() ) if ( TTF_Init() )
@ -155,10 +156,14 @@ void game::Draw()
states.back()->Draw ( this ); states.back()->Draw ( this );
} }
SDL_Window * game::GetWindow()
{
return gWindow;
}
SDL_Surface * game::GetScreen() SDL_Surface * game::GetScreen()
{ {
return m_pScreen; return gScreenSurface;
} }
bool game::Running() bool game::Running()
@ -172,6 +177,8 @@ void game::Clean()
cout << "Cleanning..." << endl; cout << "Cleanning..." << endl;
# endif # endif
SDL_DestroyWindow( gWindow );
gWindow = NULL;
TTF_CloseFont ( fontMenu ); TTF_CloseFont ( fontMenu );
TTF_Quit(); TTF_Quit();
@ -226,3 +233,8 @@ TTF_Font * game::GetfontMenu()
{ {
return fontMenu; return fontMenu;
} }
void game::SetRunning ( bool Running )
{
m_bRunning = Running;
}

View File

@ -3,11 +3,12 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include <SDL/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include "map.hpp" #include "map.hpp"
#include "part.hpp" #include "part.hpp"
#include "resource.hpp"
using namespace std; using namespace std;
@ -40,6 +41,7 @@ class game
SDL_Surface * GetScreen(); SDL_Surface * GetScreen();
Uint32 GetStart(); Uint32 GetStart();
SDL_Window * GetWindow();
TTF_Font * GetfontMenu(); TTF_Font * GetfontMenu();
TTF_Font * GetfontGame(); TTF_Font * GetfontGame();
@ -56,7 +58,8 @@ class game
void UpdateFPS(); void UpdateFPS();
private: private:
SDL_Surface * m_pScreen; SDL_Window * gWindow;
SDL_Surface * gScreenSurface;
vector <GameState *> states; vector <GameState *> states;
bool m_bRunning; bool m_bRunning;

View File

@ -1,8 +1,8 @@
#ifndef GLOBAL_HPP_INCLUDED #ifndef GLOBAL_HPP_INCLUDED
#define GLOBAL_HPP_INCLUDED #define GLOBAL_HPP_INCLUDED
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include <SDL/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include <sstream> #include <sstream>
#include <cstdio> #include <cstdio>

View File

@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <ctime> #include <ctime>
#include <cstdlib> #include <cstdlib>
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include "resource.hpp" #include "resource.hpp"
#include "global.hpp" #include "global.hpp"
@ -24,7 +24,6 @@ int main ( int argc, char ** argv )
return 0; return 0;
} }
Game.ChangeState ( MenuState::Instance() ); Game.ChangeState ( MenuState::Instance() );
while ( Game.Running() ) while ( Game.Running() )

View File

@ -3,8 +3,8 @@
#include <iostream> #include <iostream>
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include <SDL/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include "global.hpp" #include "global.hpp"
#include "game.hpp" #include "game.hpp"

View File

@ -96,7 +96,8 @@ void MenuState::Draw(game* Game)
{ {
menu->Draw(Game); menu->Draw(Game);
SDL_Flip(Game->GetScreen()); SDL_UpdateWindowSurface( Game->GetWindow() );
//SDL_Flip(Game->GetScreen());
} }

View File

@ -3,7 +3,7 @@
#include <iostream> #include <iostream>
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include "game.hpp" #include "game.hpp"
#include "gamestate.hpp" #include "gamestate.hpp"

View File

@ -2,7 +2,7 @@
#define PART_HPP_INCLUDED #define PART_HPP_INCLUDED
#include <iostream> #include <iostream>
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include "resource.hpp" #include "resource.hpp"
#include "global.hpp" #include "global.hpp"

View File

@ -89,7 +89,8 @@ void PauseState::Draw(game* Game)
{ {
menu->Draw(Game); menu->Draw(Game);
SDL_Flip(Game->GetScreen()); //SDL_Flip(Game->GetScreen());
SDL_UpdateWindowSurface( Game->GetWindow() );
} }

View File

@ -3,7 +3,7 @@
#include <iostream> #include <iostream>
#include <SDL/SDL.h> #include <SDL2/SDL.h>
#include "game.hpp" #include "game.hpp"
#include "gamestate.hpp" #include "gamestate.hpp"

View File

@ -23,7 +23,7 @@ void PlayState::Init()
textColor = make_color(255, 255, 255); textColor = make_color(255, 255, 255);
SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); // SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
# ifdef DEBUG # ifdef DEBUG
std::cout << "PlayState Init Successful" << std::endl; std::cout << "PlayState Init Successful" << std::endl;
@ -32,7 +32,7 @@ void PlayState::Init()
void PlayState::Clean() void PlayState::Clean()
{ {
SDL_EnableKeyRepeat ( 0, 0 ); // SDL_EnableKeyRepeat ( 0, 0 );
# ifdef DEBUG # ifdef DEBUG
std::cout << "PlayState Clean Successful" << std::endl; std::cout << "PlayState Clean Successful" << std::endl;
@ -41,7 +41,7 @@ void PlayState::Clean()
void PlayState::Pause() void PlayState::Pause()
{ {
SDL_EnableKeyRepeat ( 0, 0 ); // SDL_EnableKeyRepeat ( 0, 0 );
# ifdef DEBUG # ifdef DEBUG
std::cout << "PlayState Paused" << std::endl; std::cout << "PlayState Paused" << std::endl;
@ -50,7 +50,7 @@ void PlayState::Pause()
void PlayState::Resume() void PlayState::Resume()
{ {
SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); // SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
# ifdef DEBUG # ifdef DEBUG
std::cout << "PlayState Resumed" << std::endl; std::cout << "PlayState Resumed" << std::endl;
@ -226,9 +226,11 @@ void PlayState::Update ( game* Game )
{ {
/* Start enter name */ /* Start enter name */
nameEntering = true; nameEntering = true;
SDL_StartTextInput();
if (nameEntered) if (nameEntered)
{ {
SDL_StopTextInput();
nameEntering = false; nameEntering = false;
nameEntered = false; nameEntered = false;
//text.setString("Score " + intToStr(Map.getScore()) + "\n" + "Press enter"); //text.setString("Score " + intToStr(Map.getScore()) + "\n" + "Press enter");
@ -321,7 +323,8 @@ void PlayState::Draw ( game* Game )
name.show_centered(Game); name.show_centered(Game);
} }
SDL_Flip ( Game->GetScreen() ); SDL_UpdateWindowSurface( Game->GetWindow() );
//SDL_Flip ( Game->GetScreen() );
} }

View File

@ -1,7 +1,7 @@
# ifndef PLAY_STATE_HPP_INCLUDED # ifndef PLAY_STATE_HPP_INCLUDED
# define PLAY_STATE_HPP_INCLUDED # define PLAY_STATE_HPP_INCLUDED
# include <SDL/SDL.h> # include <SDL2/SDL.h>
# include <iostream> # include <iostream>
# include <string> # include <string>
# include <fstream> # include <fstream>

View File

@ -2,8 +2,8 @@
#define SCOREITEM_H #define SCOREITEM_H
# include <iostream> # include <iostream>
# include <SDL/SDL.h> # include <SDL2/SDL.h>
# include <SDL/SDL_ttf.h> # include <SDL2/SDL_ttf.h>
# include "game.hpp" # include "game.hpp"
# include "global.hpp" # include "global.hpp"

View File

@ -16,7 +16,8 @@ void ScoreState::Draw ( game * Game )
Items[i].Draw(Game, i * 35); Items[i].Draw(Game, i * 35);
} }
SDL_Flip(Game->GetScreen()); //SDL_Flip(Game->GetScreen());
SDL_UpdateWindowSurface( Game->GetWindow() );
} }
void ScoreState::Update ( game * Game ) void ScoreState::Update ( game * Game )

View File

@ -1,4 +1,4 @@
# include "playstate.hpp" #include "playstate.hpp"
# include "menustate.hpp" #include "menustate.hpp"
# include "pausestate.hpp" #include "pausestate.hpp"
# include "scorestate.hpp" #include "scorestate.hpp"

View File

@ -15,37 +15,55 @@ StringInput::~StringInput()
void StringInput::handle_input ( game * Game, SDL_Event *event) void StringInput::handle_input ( game * Game, SDL_Event *event)
{ {
string temp;
//If a key was pressed //If a key was pressed
if ( event->type == SDL_KEYDOWN ) if ( event->type == SDL_KEYDOWN )
{ {
//Keep a copy of the current version of the string //Keep a copy of the current version of the string
string temp = str; temp = str;
//If the string less than maximum size //If the string less than maximum size
if ( str.length() <= 11 ) if ( str.length() <= 11 )
{ {
//If the key is a number //Special key input
if ( ( event->key.keysym.unicode >= ( Uint16 ) '0' ) && ( event->key.keysym.unicode <= ( Uint16 ) '9' ) ) if( event->type == SDL_KEYDOWN )
{ {
//Append the character //Handle backspace
str += ( char ) event->key.keysym.unicode; if( event->key.keysym.sym == SDLK_BACKSPACE && str.length() > 0 )
{
//lop off character
str.pop_back();
} }
else if ( ( event->key.keysym.unicode == ( Uint16 ) '-' ) ) //Handle copy
else if( event->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
{ {
//Append the character SDL_SetClipboardText( str.c_str() );
str += ( char ) event->key.keysym.unicode;
} }
//If the key is a uppercase letter //Handle paste
else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'A' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'Z' ) ) else if( event->key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
{ {
//Append the character str = SDL_GetClipboardText();
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' ) )
}
}
//Special text input event
else if( event->type == SDL_TEXTINPUT )
{ {
//Append the character //Keep a copy of the current version of the string
str += ( char ) event->key.keysym.unicode; temp = str;
//If the string less than maximum size
if ( str.length() <= 11 )
{
//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;
}
} }
} }
@ -53,13 +71,6 @@ void StringInput::handle_input ( game * Game, SDL_Event *event)
cout << str << endl; cout << str << endl;
#endif #endif
//If backspace was pressed and the string isn't blank
if ( ( event->key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
{
//Remove a character from the end
str.erase ( str.length() - 1 );
}
//If the string was changed //If the string was changed
if ( str != temp ) if ( str != temp )
{ {
@ -69,8 +80,6 @@ void StringInput::handle_input ( game * Game, SDL_Event *event)
//Render a new text surface //Render a new text surface
text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor ); text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor );
} }
}
} }
void StringInput::show_centered ( game * Game ) void StringInput::show_centered ( game * Game )

View File

@ -3,8 +3,8 @@
# include <iostream> # include <iostream>
# include <string> # include <string>
# include <SDL/SDL.h> # include <SDL2/SDL.h>
# include <SDL/SDL_ttf.h> # include <SDL2/SDL_ttf.h>
# include "game.hpp" # include "game.hpp"
# include "global.hpp" # include "global.hpp"