CocosSharp for UWP

I just finished porting a game originally written in Objective-C with Cocos2d to C# with CocosSharp. Since I don't have any OSX or iOS devices anymore it has just been ported to the Android and UWP platform.

The Android version was pretty straight forward, but some work needed to be done for UWP. So if you plan to create a game with CocosSharp for UWP this post could be interesting for you.

It doesn't seem that there is someone actively working on the UWP version of CocosSharp at the moment, so I just got the latest version and fixed and added the stuff that was need for my project. Here is a list of those things:

CocosSharp (https://github.com/Andy9FromSpace/CocosSharp/tree/improvements)

MonoGame (https://github.com/Andy9FromSpace/MonoGame/tree/improvements)

  • Enabled loading of audio files (ContentManager)

I created a pull request for some of those changes, but you can also just clone my repository from https://github.com/Andy9FromSpace/CocosSharp and follow the steps in the README. Make sure you switch to branch "improvements".

And if you are interested in the outcome, you can check it out on Google Play (https://play.google.com/store/apps/details?id=ch.neonlabs.toadsworld.android) or in the Windows Store (https://www.microsoft.com/store/apps/9nblggh42cq8)

Tagged:

Posts

  • Ruslan0990Ruslan0990 DEMember ✭✭
    Wow impressive work Andy! I tested the Android app and it looks great.
  • ProcogProcog GBMember

    Hi Andy, so I downloaded you're game and it looks great. I haven't downloaded your branch but I wanted to ask whether you had any problem with pixelated graphics. The issue I have with the main branch is that on Windows Phone 8.1 / 10 the graphics are pixelated and the resolution seems low. I think this is because it is using the DUI resolution to set-up the graphics. I noticed that other people have also complained of this issue, I wondered whether you did something to resolve this in your branch?

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    Hello David

    I didn't change anything in CocosSharp in this area, but I'm setting the resolution explicitly in MainPage.xaml.cs and switching to fullscreen in App.xaml.cs. Fullscreen switching probably works different in Windows 8.1. The following code is for UWP.

    In the shared project in GameDelegate.cs I'm setting the DesignResolution and checking the screen resolution to load the correct set of sprites.

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            [...]
    
            var bounds = Window.Current.Bounds;
            GameView.Width = (int)bounds.Width;
            GameView.Height = (int)bounds.Height;
            GameView.ViewCreated += GameDelegate.LoadGame;
        }
    }
    
    sealed partial class App : Application
    {
       [...]
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            [...]
            if (e.PrelaunchActivated == false)
            {
                [...]
                ApplicationView.GetForCurrentView().FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Minimal;
                ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;
    
                Window.Current.Activate();
            }
        }   
        [...]
    }
    
    public static class GameDelegate
    {
        public static void LoadGame(object sender, EventArgs e)
        {
            CCGameView gameView = sender as CCGameView;
    
            if (gameView != null)
            {
                CCSizeI viewSize = gameView.ViewSize;
                int width = (int)(320 * Math.Min(viewSize.Width / (float)viewSize.Height, 2.0f)); // Allow max aspect ratio of 2:1
                int height = 320;
    
                gameView.DesignResolution = new CCSizeI(width, height);
    
                if (gameView.Width > 960 || gameView.Height > 640)
                {
                    CCSprite.DefaultTexelToContentSizeRatio = 4.0f;
                    gameView.ContentManager.SearchPaths = new List<string>() { "Images/XHD" };
                }
                else if (gameView.Width > 480 || gameView.Height > 320)
                {
                    CCSprite.DefaultTexelToContentSizeRatio = 2.0f;
                    gameView.ContentManager.SearchPaths = new List<string>() { "Images/HD" };
                }
                else
                {
                    CCSprite.DefaultTexelToContentSizeRatio = 1.0f;
                    gameView.ContentManager.SearchPaths = new List<string>() { "Images/LD" };
                }
    
                [...]
            }
        }
    }
    
  • ProcogProcog GBMember

    Ok so I got Andy's branch down from GitHub and built it and it works perfectly. Android version is

    https://play.google.com/store/apps/details?id=uk.co.procog.HairFlee.Droid

    Windows 10 UWP:

    https://www.microsoft.com/store/apps/9p3z0bl0v9bb

    Thanks Andy!

  • MarkedOneMarkedOne ARMember ✭✭

    Hi Andy, thanks for sharing your work, I'm having trouble with your branch. So I downloaded from the git first with 'download zip' but I can't build it, I get this:

    error : The project file could not be loaded. Could not find file 'D:\CocosSharp-improvements\MonoGame\MonoGame.Framework\MonoGame.Framework.WindowsUAP.csproj'. D:\CocosSharp-improvements\MonoGame\MonoGame.Framework\MonoGame.Framework.WindowsUAP.csproj

    It is refering to the Monogame.Framework.WindowsUAP project, I go to the folder and there isn't any csproj file, when I hit build or rebuild to the solution I get tons of compile errors probably related to the unability to load the Monogame Project. I have also installed GitHub on my windows and clone from there but same problems, any ideas?

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    Hello

    That sounds like you didn't clone the sub-modules or you didn't build the solution and project files for MonoGame with Protobuild.exe. I just pushed a small change to the submodules config in my forked repository.

    Following the steps below should help.

    1. Download windows command line tools from git-scm.com/download/win

    2. Clone repository

    git clone github.com/Andy9FromSpace/CocosSharp.git CocosSharp

    1. Checkout branch 'improvements', sync sub-modules config and clone the sub-modules too

    cd CocosSharp
    git checkout improvements
    git submodule sync
    git submodule update --init --recursive

    1. Go to folder CocosSharp\MonoGame and double-click Protobuild.exe to generate MonoGame solution and project files

    2. Launch Visual Studio and open the solution CocosSharp.WindowsUWP.sln

    3. Install the Windows SDKs if they are missing

    4. Build

    Depending on the Visual Studio version you are using the file project.json gets updated and you may have to trigger the nuget package restore manually and start the build 2-3 times until it works.

    Regards Andy

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    Somehow they don't allow me to post links anymore. So please add https : // in front of github.com/Andy9FromSpace/CocosSharp.git in the git clone command.

  • MarkedOneMarkedOne ARMember ✭✭

    Thanks Andy, I was able to compile it, I installed the GitHub plugin for VS2015 and everything downloaded correctly, it wasn't cloning the sub-modules. Everything is compiling now.

    The problem that I have now is that I don't know how to use it on my project, how do I add the reference? The dlls were created but I don't know how to use them, if I just copy them to the bin folders when I compile my project it still can't find the cocossharp reference.

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    Just a add references to the following projects in your uwp project:

    box2d.WindowsUWP
    CocosSharp.WindowsUWP
    MonoGame.Framework.WindowsUAP

    Nothing special about that. Just regular references to other projects.

  • MarkedOneMarkedOne ARMember ✭✭

    Thanks, that worked. I couldn't compile it yet because I'm modifying the code, from CCApplication to CCGameView (I had another version of CocosSharp). I want to mantain iOS, Android and WP81 intact so I'm using compiler directives for UWP but the directive WINDOWS_UWP isn't working, I'm trying to fix that now.

  • HornelchHornelch Member

    Hi Andy,

    thank you for your fantastic work, I've been looking for an Android/UWP cross-platform game environment and this is a big piece of the puzzle.

    Is there any way to make your libraries work with the CocosSharpView() from Cocossharp.Forms?

    From my limited understanding, CocosSharp.Forms always downloads its own CocosSharp library from NuGet. Therefore, I cannot add both your libraries and CocosSharp.Forms to my cross-platform project (it throws an error that there already is a reference to these DLLs).

    How would I integrate your improved CocosSharp DLLs with CocosSharp.Forms?

    Also as a related question, can I use the same DLLs for the Android sub-project of a cross-platform solution?

    Thanks in advance for your time!

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    I never used CocosSharp.Forms, but I would remove the CocosSharp.Forms nuget and reference the CocosSharp.Forms project/DLL from the CocosSharp solution directly.

    CocosSharp/CocosSharp.Forms/
    CocosSharp.Forms.Android
    CocosSharp.Forms.PCL
    CocosSharp.Forms.WP
    CocosSharp.Forms.iOS

    I'm not sure if I get you second question. But always reference the CocosSharp project/DLL that matches your platform.

  • HornelchHornelch Member

    Cheers for the super-fast reply!

    To follow up your comments:

    Following the advice you gave to MarkedOne above, I have no DLLs or projects for CocosSharp.Forms in the clone of your repository in the folder git\CocosSharp\CocosSharp.Forms and its subfolders, so I cannot add references there.

    My second was question was related to the 3 projects in my solution:
    CCSharpTest
    CCSharpTest.Android
    CCSharpTest.UWP

    I need references to cocossharp in all 3. Where do I add your improved DLLs, and where do I add the standard 1.7.1 references?

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    When I clone the CocosSharp repository I have a solution and some projects in the CocosSharp.Forms sub folder. But it doesn't have any UWP project and I get a couple of errors when opening the solution. So I think you better not include anything from that folder.

    I included the projects as you see below and everything worked just fine:

    CCSharpTest (Shared project)
    CCSharpTest.Android
        box2d.Android (<GitFolder>\CocosSharp\box2d\box2d.Android.csproj)
        CocosSharp.Android (<GitFolder>\CocosSharp\Core\CocosSharp.Android.csproj)
        MonoGame.Framework.Android (<GitFolder>\CocosSharp\MonoGame\MonoGame.Framework\MonoGame.Framework.Android.csproj)
    CCSharpTest.UWP
        box2d.WindowsUWP (<GitFolder>\CocosSharp\box2d\box2d.WindowsUWP.csproj)
        CocosSharp.WindowsUWP (<GitFolder>\CocosSharp\Core\CocosSharp.WindowsUWP.csproj)
        MonoGame.Framework.WindowsUAP (<GitFolder>\CocosSharp\MonoGame\MonoGame.Framework\MonoGame.Framework.WindowsUAP.csproj)
    
  • ArlecchinoArlecchino USMember ✭✭
    edited March 2018

    @Andy9FromSpace So there is no way to use CocosSharp.Forms in an UWP project currently? Can you clarify this, please?

  • Andy9FromSpaceAndy9FromSpace CHMember ✭✭

    I just checked out CocosSharp.Forms quickly and there wasn't any project related to UWP. There's the project CocosSharp.Forms.WP and that's probably a good start to make it work with UWP. But I have no idea how much work that would be.

  • LaetitiaLaetitia Member ✭✭

    Thank you for this amazing work! I will take a look at your repo, and hopefully I will be able to compile my projects for the UWP platform too :-)

  • LaetitiaLaetitia Member ✭✭

    @Andy9FromSpace

    It seems the change you made to allow loading of audio files totally breaks loading files as XNB

    I had to revert it because somehow it was breaking my project :-)

    Still, I can't wait to try the feature you implemented! :smile:

  • Junior_OliveiraJunior_Oliveira USMember ✭✭

    @Andy9FromSpace

    Good work! Congratulations and thank you...

    I'm using C Sharp with Xamarin forms, I'm having problems with the cache, have you gone through this?:

    In App:

    public CCGameView gameView;

    Use this way:

    App.Current.gameView = sender as CCGameView;
    
    App.Current.gameView.DesignResolution = new CCSizeI(App.SceneWidth, App.SceneHeight);
    App.Current.gameView.ResolutionPolicy = CCViewResolutionPolicy.FixedWidth;
    App.Current.gameView.ContentManager.SearchPaths = new List<string>() { "Fonts", "Sounds", "Images"}
    App.Current.gameView.TextureCache.AddImage("Image1");
    App.Current.gameView.TextureCache.AddImage("Image2");
    App.Current.gameView.TextureCache.AddImage("Image3");
    
        gameScene = new GameScene(this, App.Current.gameView);
        App.Current.gameView.RunWithScene(gameScene);
    

    To unload the images I tried some features, without result:

    App.Current.gameView.TextureCache.Dispose();
    App.Current.gameView.TextureCache.UnloadContent();
    

    Can you help me please I to see where I am wrong,
    and sorry my English.

Sign In or Register to comment.