This commit is contained in:
2026-02-13 14:09:12 +08:00
commit 6ed1953e24
3 changed files with 430 additions and 0 deletions

172
CMakeLists.txt Normal file
View File

@@ -0,0 +1,172 @@
#/****************************************************************************
# Copyright (c) 2013-2014 cocos2d-x.org
#
# http://www.cocos2d-x.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ****************************************************************************/
cmake_policy(SET CMP0017 NEW)
cmake_minimum_required(VERSION 2.8)
set(APP_NAME MyGame)
project (${APP_NAME})
set(COCOS2D_ROOT ${CMAKE_SOURCE_DIR}/cocos2d)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${COCOS2D_ROOT}/cmake/Modules/")
include(CocosBuildHelpers)
# libcocos2d
set(BUILD_CPP_TESTS OFF CACHE BOOL "turn off build cpp-tests")
set(BUILD_LUA_LIBS OFF CACHE BOOL "turn off build lua related targets")
set(BUILD_JS_LIBS OFF CACHE BOOL "turn off build js related targets")
add_subdirectory(${COCOS2D_ROOT})
# Some macro definitions
if(WINDOWS)
if(BUILD_SHARED_LIBS)
ADD_DEFINITIONS (-D_USRDLL -D_EXPORT_DLL_ -D_USEGUIDLL -D_USREXDLL -D_USRSTUDIODLL)
else()
ADD_DEFINITIONS (-DCC_STATIC)
endif()
ADD_DEFINITIONS (-DCOCOS2DXWIN32_EXPORTS -D_WINDOWS -DWIN32 -D_WIN32)
set(PLATFORM_FOLDER win32)
elseif(MACOSX OR APPLE)
ADD_DEFINITIONS (-DCC_TARGET_OS_MAC)
ADD_DEFINITIONS (-DUSE_FILE32API)
set(PLATFORM_FOLDER mac)
elseif(LINUX)
ADD_DEFINITIONS(-DLINUX)
set(PLATFORM_FOLDER linux)
elseif(ANDROID)
ADD_DEFINITIONS (-DUSE_FILE32API)
set(PLATFORM_FOLDER android)
else()
message( FATAL_ERROR "Unsupported platform, CMake will exit" )
endif()
# Compiler options
if(MSVC)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS
-wd4251 -wd4244 -wd4334 -wd4005 -wd4820 -wd4710
-wd4514 -wd4056 -wd4996 -wd4099)
else()
set(CMAKE_C_FLAGS_DEBUG "-g -Wall -DCOCOS2D_DEBUG=1")
set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-deprecated-declarations -Wno-reorder")
if(CLANG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
endif(MSVC)
set(PLATFORM_SPECIFIC_SRC)
set(PLATFORM_SPECIFIC_HEADERS)
if(MACOSX OR APPLE)
set(PLATFORM_SPECIFIC_SRC
proj.ios_mac/ios/main.m
proj.ios_mac/ios/RootViewController.mm
proj.ios_mac/ios/AppController.mm
)
set(PLATFORM_SPECIFIC_HEADERS
proj.ios_mac/ios/RootViewController.h
proj.ios_mac/ios/AppController.h
)
elseif(LINUX) #assume linux
set(PLATFORM_SPECIFIC_SRC
proj.linux/main.cpp
)
elseif ( WIN32 )
set(PLATFORM_SPECIFIC_SRC
proj.win32/main.cpp
)
set(PLATFORM_SPECIFIC_HEADERS
proj.win32/main.h
proj.win32/resource.h
)
endif()
include_directories(
/usr/local/include/GLFW
/usr/include/GLFW
${COCOS2D_ROOT}/cocos
${COCOS2D_ROOT}/cocos/platform
${COCOS2D_ROOT}/cocos/audio/include/
Classes
)
if ( WIN32 )
include_directories(
${COCOS2D_ROOT}/external/glfw3/include/win32
${COCOS2D_ROOT}/external/win32-specific/gles/include/OGLES
)
endif( WIN32 )
set(GAME_SRC
Classes/AppDelegate.cpp
Classes/HelloWorldScene.cpp
${PLATFORM_SPECIFIC_SRC}
)
set(GAME_HEADERS
Classes/AppDelegate.h
Classes/HelloWorldScene.h
${PLATFORM_SPECIFIC_HEADERS}
)
if(GAME_HEADERS)
if ( WIN32 )
add_executable(${APP_NAME} WIN32 ${GAME_SRC} ${GAME_HEADERS})
else()
add_executable(${APP_NAME} ${GAME_SRC} ${GAME_HEADERS})
endif ( WIN32 )
else()
if ( WIN32 )
add_executable(${APP_NAME} WIN32 ${GAME_SRC})
else()
add_executable(${APP_NAME} ${GAME_SRC})
endif ( WIN32 )
endif()
target_link_libraries(${APP_NAME} cocos2d)
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin")
set_target_properties(${APP_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}")
if ( WIN32 )
#also copying dlls to binary directory for the executable to run
pre_build(${APP_NAME}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources
COMMAND ${CMAKE_COMMAND} -E copy ${COCOS2D_ROOT}/external/win32-specific/gles/prebuilt/glew32.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}
COMMAND ${CMAKE_COMMAND} -E copy ${COCOS2D_ROOT}/external/win32-specific/zlib/prebuilt/zlib1.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}
)
else()
pre_build(${APP_NAME}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources
)
endif()

211
Classes/AppDelegate.cpp Normal file
View File

@@ -0,0 +1,211 @@
#include "AppDelegate.h"
#include "MainScene.h"
#include "GameFrameBase.h"
#include "LogoScene.h"
#include "LogonScene.h"
#include "GlobalJosn.h"
#include "ChatScene.h"
#include "GameCreator.h"
//#include "WN_CreateScene.h"
#include "WN_GameScene.h"
#include "DDZ_GameScene.h"
#include "NN_GameScene.h"
#include "DZ_GameScene.h"
#include "13S_GameScene.h"
#include "ZJH_GameScene.h"
#include "PDK_GameScene.h"
USING_NS_CC;
/*******************************注册游戏****************************************/
// 游戏选项
struct tagGameItem
{
WORD wKindID;
GAME_CREATE_SELECTOR create;
GAME_CREATE_NODE createNode;
};
static tagGameItem s_GameList[] =
{
// 斗地主;
{ DDZ_KIND_ID, GAME_CREATE_SELECTOR(DDZ_SPACE::DDZGameScene::create), GAME_CREATE_NODE(nullptr) },
// 万年麻将;
{ WNMJ_SPACE::KIND_ID, GAME_CREATE_SELECTOR(WNMJ_SPACE::WN_GameScene::create), GAME_CREATE_NODE(nullptr) },
// 牛牛;
{ NN_KIND_ID, GAME_CREATE_SELECTOR(NiuNiu_SPACE::NNGameScene::create), GAME_CREATE_NODE(nullptr) },
// 打炸;
{ DZ_KIND_ID, GAME_CREATE_SELECTOR(DZ_SPACE::DZGameScene::create), GAME_CREATE_NODE(nullptr) },
// 十三水;
{ SSS_KIND_ID, GAME_CREATE_SELECTOR(SSS_SPACE::SSSGameScene::create), GAME_CREATE_NODE(nullptr) },
// 炸金花;
{ ZJH_KIND_ID, GAME_CREATE_SELECTOR(ZJH_SPACE::ZJHGameScene::create), GAME_CREATE_NODE(nullptr) },
// 跑得快;
{ PDK_KIND_ID, GAME_CREATE_SELECTOR(PDK_SPACE::PDKGameScene::create), GAME_CREATE_NODE(nullptr) },
};
/******************************************************************************/
AppDelegate::AppDelegate()
{
m_dispathMsgNode = NULL;
}
AppDelegate::~AppDelegate()
{
//// 释放资源;
//YvVoiceManager::GetInstance()->Cleanup();
if (m_dispathMsgNode != NULL)
{
m_dispathMsgNode->stopDispatch();
m_dispathMsgNode->release();
m_dispathMsgNode = NULL;
}
}
//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// If you want to use packages manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("GameClient", Rect(0, 0, TARGET_WIDTH, TARGET_HEIGHT));
#else
glview = GLViewImpl::create("GameClient");
#endif
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(false);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / 60);
//设置资源搜索路径
//FileUtils::getInstance()->addSearchPath("UI/");
//FileUtils::getInstance()->addSearchPath("NCMJ/");
register_all_packages();
initGameConfig();
GlobalJosn::getInstance()->loadConfig();
//设置常亮不锁屏
Device::setKeepScreenOn(true);
// create a scene. it's an autorelease object
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32||CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
Scene *pScene = LogonScene::create();
srand(time(0));
// run
director->runWithScene(pScene);
#else
Scene *pScene = LogoScene::create();
if (pScene == nullptr)
{
pScene = LogonScene::create();
}
srand(time(0));
// run
director->runWithScene(pScene);
#endif
glview->setDesignResolutionSize(TARGET_WIDTH, TARGET_HEIGHT, ResolutionPolicy::EXACT_FIT);
if (m_dispathMsgNode == NULL)
{
m_dispathMsgNode = DispatchMsgNode::create();
m_dispathMsgNode->retain();
m_dispathMsgNode->startDispatch();
}
YvVoiceManager::GetInstance()->Init();
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32
Director::getInstance()->stopAnimation();
Scene* pScene = Director::getInstance()->getRunningScene();
if(pScene!=nullptr && pScene->getTag()==SCENE_TAG_GAME)
{
//applicationDidEnterBackground();
}
#endif
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
//设置常亮不锁屏
Device::setKeepScreenOn(true);
Scene* pScene = Director::getInstance()->getRunningScene();
if (pScene != nullptr)
{
int nTag = pScene->getTag();
if ( nTag == SCENE_TAG_GAME )
{
GameFrameBase* pMJScene = (GameFrameBase*)pScene;
pMJScene->ReconnectServer();
}
else if ( nTag == SCENE_TAG_MAIN )
{
MainScene* pMainScene = (MainScene*)pScene;
pMainScene->appWillEnterForeground();
}
}
#endif
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
void AppDelegate::initGameConfig()
{
// 注册游戏
int gameCount = sizeof(s_GameList) / sizeof(tagGameItem);
for (int i = 0; i < gameCount; i++)
{
CGameCreator::getInstance()->addGame(s_GameList[i].wKindID, CGameCreator::PRIVATE, s_GameList[i].create, s_GameList[i].createNode);
}
}

47
Classes/AppDelegate.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
#include "DispatchMsgNode.hpp"
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
// ÓÎÏ·³õʼ»¯
void initGameConfig();
private:
DispatchMsgNode * m_dispathMsgNode;
};
#endif // _APP_DELEGATE_H_