Resize Window Using GLFW3

When porting our game from mobile to desktop we wanted to add a few desktop specific interface enhancements. Things like using the keyboard, mouse, or managing the Window. By default games are setup to run full screen instead of within a window on desktop platforms, but since our game is pixel art full screen is a bit large on modern 24″+ monitors. So we’re sticking with running inside a window for now.

Ideally we’d allow players to resize the window. In order to support resizing a running scene we would need support to dynamically update every single UI element. Each scene or menu does size, scale, and position UI widgets on the current device (or now window) size, so we will allow changing resolution from a menu and then we will just reload that menu with replaceScene() after the changing the window frame size. We could build in support for this and attempt to unify the changes into a base class or small set of resize methods, but it would be better done at the first design and creation phase of a game’s development.

/// glview - the cocos2d-x platform OpenGL view reference 
/// frameSize - the desired window size
void OptionsResolutionMenu::static_changeResolution(GLViewImpl* glview, Size frameSize)
{
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX))
    auto glfwindow = glview->getWindow();
    auto fsize = glview->getFrameSize();
    auto screenSize = glview->getMonitorSize();
   
    int sw = screenSize.width > 0 ? (int)screenSize.width : 800;
    int sh = screenSize.height > 0 ? (int)screenSize.height : 480;
  
    // win position
    int winx = 0, winy = 0;
    glfwGetWindowPos(glfwindow, &winx, &winy);

    Rect viewport = glview->getViewPortRect();
    Vec2 viewcenter = Vec2(viewport.getMidX(), viewport.getMidY());

    // update the cocos2d view frame
    glview->setFrameSize(frameSize.width, frameSize.height);

    int x = int(sw - frameSize.width)/2;
    int y = int(sh - frameSize.height)/2;
    glfwSetWindowPos(glfwindow, x, y);
#endif
}
// call method with desired size (we get this from user options menu)
float resWidth = 800;
float resHeight = 600; 
OptionsResolutionMenu::static_changeResolution(glviewImpl, Size(resWidth, resHeight));
GameManager::getInstance()->runSceneWithID(kSceneOptionsResolution, 0);

[ST.2015.033]

The Proverbial Meta Post – The Why?

Ask yourself Why?

Sharing this “why” publicly also puts you in a place where you are forced to defend it … If someone asked you why you write, could you adequately, soundly, and confidently give them a reply that was satisfactory?

This reaffirmed that I should write a meta blog post. Basically it always feels like wasted space. His reminder similar to the Oracle’s[in The Matrix] question, “Are you ready?” and after Neo says no she translates the message “Know Thyself.” Why am I writing? Why do I want to write?

The Why?

Writing is something I enjoy. It’s a creative effort also requiring analytical thinking. A serious description can be followed by a sarcastic commentary back and forth and end with metaphorical crossing the finish line.

About Page Refactor

He suggests writing an ‘About’ page for day 6. Since I’ve already completed that exercise I will take one day during the next few weeks to refactor [ref: the process of restructuring code when programming] and update my about page. Afterward I will post a link with a short list of important aspects or changes I made.

The What

This blog is where I can put my thoughts down and write about anything, but the content will likely relate mostly to making and doing. Whether it’s software development (apps/games/web), technology, business, gadgets, workflow, process, music, art, or tools. My main goal is to deliver or relay useful information to at least one other person, but at least it’s a reference for myself to remember or look back on my own thoughts, ideas, or creations.

There are many times where I think about starting a specific game development blog, or a business blog, or others, but the problem with following them is that I first need to build up a habit of writing and eventually writing well.

That’s the other main purpose for starting up writing again. I’d like to improve and become a more proficient writer. Increase my vocabulary. Learn which words to use in which context. Find a voice that will help convey my intentions.

The How

Currently I’m using WordPress and have converted my theme over to the default one since I’m not focused on design or designing my blog. Also, for now I will focus on content and not the technology. Static blog engines are enticing because they scratch the programmer’s itch, and maybe I’ll test out a few in the future and write a review of each.

That said I am going to improve things a bit where needed. Notably using github’s embedded gists or finding a plugin for code highlighting.

[ST.2015.030]

Taking a Day of Rest for the Ten Day Challenge

My goal for the first week was to not write anything meta. I have planned to eventually write a post on a few why questions, as well as some what and how, but was hoping to wait a while since it always feels a bit of wasted writing.

Then I stumbled through a series of tubes, as one usually does when checking their twitter feed, and found myself reading about John’s 10 day blogging challenge. I’m not joining in on that project officially since it’s almost over, but at least I can add a few thoughts on a few of his challenges.

Rest
He mentions taking a day of rest for the 4th day. I would like to write daily, but I know that after a while I’ll find it difficult to stay motivated to take the time for writing. This was a good reminder that I should still strive for a post per day, but maybe I don’t have to limit it to writing only one per day always at night.

Writing in the morning may help. Finding a mobile app setup for easy posting could help. But I may allow myself to schedule posts in the future as well. I won’t write 20 of them and take 2 weeks off, but I think writing 3-4 one day and taking a couple off is a good idea. Also, maybe build up a 5 post reserve for those days where I can’t think of anything useful to write about, or don’t take the time, or can only think of ideas requiring a long article.

Tomorrow I’ll write all about Why?

[ST.2015.027]

Adding OS X Menu Bar with cocos2d-x 3.3

[todo: convert into a gist, and cleanup post]

I’ll post discussion topics here discussing adding or changing the Menu, but I found out adding MainMenu.xib didn’t work correctly as I have thought during the discussions over on the forum.

Looking into where the menu is created I found out that GLFW3 actually creates its own default menu with only two Menus services(App menu) and Window. The code where it sets this menu up is found in cocoa_window.m over on github.

Once I figured this out I had to investigate how to edit the app or window menu. Looking over the window code, and searching through the various header files from within XCode I was able to add an item to the Window menu.

Update 2015-01-17: I’ve posted up an example project using cocos2d-x 3.3


void STDeviceMac::addMainMenu(GLViewImpl* glview)
{
NSWindow * appWindow = (NSWindow )glfwGetCocoaWindow(glview->getWindow());
if(appWindow)
{
// make your obj-c calls here
NSMenu
menu = [NSApp windowsMenu];
{
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:@"Check for Updates"
action:@selector(checkForUpdates:)
keyEquivalent:@""];
[menuItem setTarget:[SUUpdater sharedUpdater]];
[menu addItem:menuItem];
}
{
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:@"Steve's Test"
action:@selector(test:)
keyEquivalent:@""];
[menuItem setTarget:_deviceImpl];
[menu addItem:menuItem];
}
}
}

[ST.2015.025]