Skip to content

Commit 85cdb87

Browse files
author
Alex Ames
authored
Merge pull request firebase#49 from Grant-Postma/database-setup
Database setup Hooked up Firebase Database functionality into Tic Tac Toe game. Tested on Windows.
2 parents 2f9eea5 + 8bf3cf0 commit 85cdb87

File tree

11 files changed

+912
-90
lines changed

11 files changed

+912
-90
lines changed

demos/TicTacToe/CMakeLists.txt

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#/****************************************************************************
2+
# Copyright (c) 2013-2014 cocos2d-x.org
3+
# Copyright (c) 2015-2017 Chukong Technologies Inc.
4+
#
5+
# http://www.cocos2d-x.org
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
# ****************************************************************************/
25+
26+
cmake_minimum_required(VERSION 3.6)
27+
28+
set(APP_NAME tictactoe_demo)
29+
30+
project(${APP_NAME})
31+
32+
# User settings for Firebase samples.
33+
# Path to Firebase SDK.
34+
# Try to read the path to the Firebase C++ SDK from an environment variable.
35+
if (NOT "$ENV{FIREBASE_CPP_SDK_DIR}" STREQUAL "")
36+
set(DEFAULT_FIREBASE_CPP_SDK_DIR "$ENV{FIREBASE_CPP_SDK_DIR}")
37+
else()
38+
set(DEFAULT_FIREBASE_CPP_SDK_DIR "firebase_cpp_sdk")
39+
endif()
40+
if ("${FIREBASE_CPP_SDK_DIR}" STREQUAL "")
41+
set(FIREBASE_CPP_SDK_DIR ${DEFAULT_FIREBASE_CPP_SDK_DIR})
42+
endif()
43+
if(NOT EXISTS ${FIREBASE_CPP_SDK_DIR})
44+
message(FATAL_ERROR "The Firebase C++ SDK directory does not exist: ${FIREBASE_CPP_SDK_DIR}. See the readme.md for more information")
45+
endif()
46+
47+
# Build a desktop application.
48+
# Windows runtime mode, either MD or MT depending on whether you are using
49+
# /MD or /MT. For more information see:
50+
# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
51+
set(MSVC_RUNTIME_MODE MD)
52+
53+
if(APPLE)
54+
set(ADDITIONAL_LIBS gssapi_krb5 pthread "-framework CoreFoundation" "-framework Foundation" "-framework GSS" "-framework Security")
55+
elseif(MSVC)
56+
set(ADDITIONAL_LIBS advapi32 ws2_32 crypt32 iphlpapi psapi userenv)
57+
else()
58+
set(ADDITIONAL_LIBS pthread)
59+
endif()
60+
61+
# If a config file is present, copy it into the binary location so that it's
62+
# possible to create the default Firebase app.
63+
set(FOUND_JSON_FILE FALSE)
64+
foreach(config "google-services-desktop.json" "google-services.json")
65+
if (EXISTS ${config})
66+
add_custom_command(
67+
TARGET ${APP_NAME} POST_BUILD
68+
COMMAND ${CMAKE_COMMAND} -E copy
69+
${config} $<TARGET_FILE_DIR:${APP_NAME}>)
70+
set(FOUND_JSON_FILE TRUE)
71+
break()
72+
endif()
73+
endforeach()
74+
if(NOT FOUND_JSON_FILE)
75+
message(WARNING "Failed to find either google-services-desktop.json or google-services.json. See the readme.md for more information.")
76+
endif()
77+
#Target name change to cocos
78+
# Add the Firebase libraries to the target using the function from the SDK.
79+
add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
80+
81+
# # Note that firebase_app needs to be last in the list.
82+
set(firebase_libs firebase_database firebase_auth firebase_app)
83+
84+
if(XCODE)
85+
if(NOT DEFINED CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET)
86+
SET (CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET 8.0)
87+
endif()
88+
endif()
89+
90+
if(NOT DEFINED BUILD_ENGINE_DONE) # to test install_test into root project
91+
set(COCOS2DX_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cocos2d)
92+
set(CMAKE_MODULE_PATH ${COCOS2DX_ROOT_PATH}/cmake/Modules/)
93+
94+
include(CocosBuildSet)
95+
add_subdirectory(${COCOS2DX_ROOT_PATH}/cocos ${ENGINE_BINARY_PATH}/cocos/core)
96+
endif()
97+
98+
# record sources, headers, resources...
99+
set(GAME_HEADER)
100+
101+
set(GAME_RES_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/Resources")
102+
103+
if(APPLE OR WINDOWS)
104+
cocos_mark_multi_resources(common_res_files RES_TO "Resources" FOLDERS ${GAME_RES_FOLDER})
105+
endif()
106+
107+
# add cross-platforms source files and header files
108+
list(APPEND GAME_SOURCE Classes/AppDelegate.cpp Classes/TicTacToeScene.cpp)
109+
list(APPEND GAME_HEADER Classes/AppDelegate.h Classes/TicTacToeScene.h)
110+
111+
if(ANDROID)
112+
# change APP_NAME to the share library name for Android, it's value depend on AndroidManifest.xml
113+
set(APP_NAME MyGame)
114+
list(APPEND GAME_SOURCE proj.android/app/jni/hellocpp/main.cpp)
115+
elseif(LINUX)
116+
list(APPEND GAME_SOURCE proj.linux/main.cpp)
117+
elseif(WINDOWS)
118+
list(APPEND GAME_HEADER proj.win32/main.h proj.win32/resource.h)
119+
list(APPEND GAME_SOURCE proj.win32/main.cpp proj.win32/game.rc ${common_res_files})
120+
elseif(APPLE)
121+
if(IOS)
122+
list(APPEND GAME_HEADER proj.ios_mac/ios/AppController.h
123+
proj.ios_mac/ios/RootViewController.h)
124+
set(APP_UI_RES proj.ios_mac/ios/LaunchScreen.storyboard
125+
proj.ios_mac/ios/LaunchScreenBackground.png
126+
proj.ios_mac/ios/Images.xcassets)
127+
list(APPEND GAME_SOURCE proj.ios_mac/ios/main.m proj.ios_mac/ios/AppController.mm
128+
proj.ios_mac/ios/RootViewController.mm proj.ios_mac/ios/Prefix.pch ${APP_UI_RES})
129+
elseif(MACOSX)
130+
set(APP_UI_RES proj.ios_mac/mac/Icon.icns proj.ios_mac/mac/Info.plist)
131+
list(APPEND GAME_SOURCE proj.ios_mac/mac/main.cpp
132+
proj.ios_mac/mac/Prefix.pch ${APP_UI_RES})
133+
endif()
134+
list(APPEND GAME_SOURCE ${common_res_files})
135+
endif()
136+
137+
# mark app complie info and libs info
138+
set(all_code_files ${GAME_HEADER} ${GAME_SOURCE})
139+
if(NOT ANDROID)
140+
add_executable(${APP_NAME} ${all_code_files})
141+
else()
142+
add_library(${APP_NAME} SHARED ${all_code_files})
143+
add_subdirectory(${COCOS2DX_ROOT_PATH}/cocos/platform/android
144+
${ENGINE_BINARY_PATH}/cocos/platform)
145+
target_link_libraries(${APP_NAME} -Wl,--whole-archive cpp_android_spec
146+
-Wl,--no-whole-archive)
147+
endif()
148+
149+
target_link_libraries(${APP_NAME} cocos2d "${firebase_libs}" ${ADDITIONAL_LIBS})
150+
target_include_directories(${APP_NAME} PRIVATE Classes
151+
PRIVATE ${COCOS2DX_ROOT_PATH}/cocos/audio/include/)
152+
153+
# mark app resources
154+
setup_cocos_app_config(${APP_NAME})
155+
if(APPLE)
156+
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
157+
if(MACOSX)
158+
set_xcode_property(${APP_NAME} INFOPLIST_FILE
159+
"${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/mac/Info.plist")
160+
elseif(IOS)
161+
set_xcode_property(${APP_NAME} INFOPLIST_FILE
162+
"${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/ios/Info.plist")
163+
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
164+
endif()
165+
166+
# For code-signing, set the DEVELOPMENT_TEAM:
167+
#set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "GRLXXXX2K9")
168+
elseif(WINDOWS)
169+
cocos_copy_target_dll(${APP_NAME})
170+
endif()
171+
172+
if(LINUX OR WINDOWS)
173+
cocos_get_resource_path(APP_RES_DIR ${APP_NAME})
174+
cocos_copy_target_res(${APP_NAME} LINK_TO ${APP_RES_DIR}
175+
FOLDERS ${GAME_RES_FOLDER})
176+
endif()
177+

demos/TicTacToe/Classes/AppDelegate.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "AppDelegate.h"
22

3+
#include "MainMenuScene.h"
34
#include "TicTacToeScene.h"
4-
55
USING_NS_CC;
66

77
const float kFrameWidth = 600;
@@ -10,7 +10,6 @@ const float kFrameHeight = 600;
1010
AppDelegate::AppDelegate() {}
1111

1212
AppDelegate::~AppDelegate() {}
13-
1413
bool AppDelegate::applicationDidFinishLaunching() {
1514
auto director = Director::getInstance();
1615
auto glview = director->getOpenGLView();
@@ -20,7 +19,7 @@ bool AppDelegate::applicationDidFinishLaunching() {
2019
director->setOpenGLView(glview);
2120
}
2221

23-
auto scene = TicTacToe::createScene();
22+
auto scene = MainMenuScene::createScene();
2423
director->runWithScene(scene);
2524

2625
return true;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "MainMenuScene.h"
2+
3+
#include "TicTacToeScene.h"
4+
5+
USING_NS_CC;
6+
7+
Scene* MainMenuScene::createScene() {
8+
// Builds a simple scene that uses the bottom left cordinate point as (0,0)
9+
// and can have sprites, labels and layers added onto it.
10+
auto scene = Scene::create();
11+
auto layer = MainMenuScene::create();
12+
13+
scene->addChild(layer);
14+
15+
return scene;
16+
}
17+
18+
bool MainMenuScene::init() {
19+
if (!Layer::init()) {
20+
return false;
21+
}
22+
// Creates a sprite for the create button and sets its position to the center
23+
// of the screen. TODO(grantpostma): Dynamically choose the location.
24+
auto create_button = Sprite::create("create_game.png");
25+
create_button->setPosition(300, 350);
26+
// Create a button listener to handle the touch event.
27+
auto create_button_touch_listener = EventListenerTouchOneByOne::create();
28+
// Setting the onTouchBegan event up to a lambda tha will replace the MainMenu
29+
// scene with a TicTacToe scene.
30+
create_button_touch_listener->onTouchBegan = [](Touch* touch,
31+
Event* event) -> bool {
32+
auto bounds = event->getCurrentTarget()->getBoundingBox();
33+
auto point = touch->getLocation();
34+
// Replaces the scene with a new TicTacToe scene if the touched point is
35+
// within the bounds of the button.
36+
if (bounds.containsPoint(point)) {
37+
Director::getInstance()->replaceScene(
38+
TicTacToe::createScene(std::string()));
39+
}
40+
41+
return true;
42+
};
43+
// Attaching the touch listener to the create game button.
44+
Director::getInstance()
45+
->getEventDispatcher()
46+
->addEventListenerWithSceneGraphPriority(create_button_touch_listener,
47+
create_button);
48+
49+
// Creating, setting the position and assigning a placeholder to the text
50+
// field for entering the join game uuid.
51+
TextFieldTTF* join_text_field =
52+
cocos2d::TextFieldTTF::textFieldWithPlaceHolder(
53+
"Join Game url", cocos2d::Size(400, 200), TextHAlignment::RIGHT,
54+
"Arial", 42.0);
55+
join_text_field->setPosition(100, 100);
56+
join_text_field->setColorSpaceHolder(Color3B::GRAY);
57+
join_text_field->setDelegate(this);
58+
59+
// Create a touch listener to handle the touch event. TODO(grantpostma): add a
60+
// focus bar when selecting inside the text field's bounding box.
61+
auto text_field_touch_listener = EventListenerTouchOneByOne::create();
62+
63+
text_field_touch_listener->onTouchBegan =
64+
[join_text_field](cocos2d::Touch* touch, cocos2d::Event* event) -> bool {
65+
auto bounds = event->getCurrentTarget()->getBoundingBox();
66+
auto point = touch->getLocation();
67+
if (bounds.containsPoint(point)) {
68+
// Show the on screen keyboard and places character inputs into the text
69+
// field.
70+
auto str = join_text_field->getString();
71+
auto textField = dynamic_cast<TextFieldTTF*>(event->getCurrentTarget());
72+
textField->attachWithIME();
73+
}
74+
75+
return true;
76+
};
77+
78+
// Attaching the touch listener to the text field.
79+
Director::getInstance()
80+
->getEventDispatcher()
81+
->addEventListenerWithSceneGraphPriority(text_field_touch_listener,
82+
join_text_field);
83+
84+
// Creates a sprite for the join button and sets its position to the center
85+
// of the screen. TODO(grantpostma): Dynamically choose the location.
86+
auto join_button = Sprite::create("join_game.png");
87+
join_button->setPosition(450, 100);
88+
89+
// Create a button listener to handle the touch event.
90+
auto join_button_touch_listener = EventListenerTouchOneByOne::create();
91+
// Setting the onTouchBegan event up to a lambda tha will replace the MainMenu
92+
// scene with a TicTacToe scene and pass in join_text_field string.
93+
join_button_touch_listener->onTouchBegan =
94+
[join_text_field](Touch* touch, Event* event) -> bool {
95+
auto bounds = event->getCurrentTarget()->getBoundingBox();
96+
auto point = touch->getLocation();
97+
if (bounds.containsPoint(point)) {
98+
// Getting and converting the join_text_field string to a char*.
99+
std::string join_text_field_string = join_text_field->getString();
100+
101+
Director::getInstance()->replaceScene(
102+
TicTacToe::createScene(join_text_field_string));
103+
}
104+
return true;
105+
};
106+
// Attaching the touch listener to the join button.
107+
Director::getInstance()
108+
->getEventDispatcher()
109+
->addEventListenerWithSceneGraphPriority(join_button_touch_listener,
110+
join_button);
111+
// Attaching the create button, join button and join text field to the
112+
// MainMenu scene.
113+
this->addChild(create_button);
114+
this->addChild(join_button);
115+
this->addChild(join_text_field);
116+
117+
return true;
118+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_
2+
#define TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_
3+
4+
#include "cocos2d.h"
5+
#include "ui/CocosGUI.h"
6+
7+
class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate {
8+
public:
9+
// Builds a simple scene that uses the bottom left cordinate point as (0,0)
10+
// and can have sprites, labels and nodes added onto it.
11+
static cocos2d::Scene* createScene();
12+
// Initializes the instance of a Node and returns a boolean based on if it was
13+
// successful in doing so.
14+
virtual bool init();
15+
// Defines a create type for a specific type, in this case a Layer.
16+
CREATE_FUNC(MainMenuScene);
17+
};
18+
19+
#endif // TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_

0 commit comments

Comments
 (0)