Categories
Geek / Technical

Fixed SDL2 Android App Issue When Suspending and Resuming

Months ago, I needed to update the SDL2 libraries I was using in order to gain access to a new piece of functionality in SDL2 that lets me load SVG vector graphic file and create textures out of them. But then I found a problem was introduced. The app would hang whenever I would leave it to look at a different app. I posted about it on the libSDL Discourse site, but I did not receive a response. I finally had a chance to investigate this issue and I think I have solved it.

When an SDL2-based Android app needs to suspend and resume, there are events that need to be handled immediately, before your main event loop: SDL_APP_WILLENTERBACKGROUND and SDL_APP_DIDENTERFOREGROUND.

So you create an event filter, register it with SDL_SetEventFilter(), and it is here that you need to handle things like music. If your app enters the background, but music is still playing, the app will crash. So, in your event filter, you would have code that looks something like the following:

int EventHandler::eventFilter(void * userData, SDL_Event * event)
{
    int whatToDoWithThisEvent(ADD_TO_QUEUE);

    IHardwareLayer * hardwareLayer = static_cast<IHardwareLayer *>(userData);
    if (hardwareLayer != 0) 
    { 
        switch(event->type)
        {
            case SDL_APP_WILLENTERBACKGROUND:
                {
                    hardwareLayer->pauseMusic();
                    whatToDoWithThisEvent = DO_NOT_ADD_TO_QUEUE;
                }
                break;

            case SDL_APP_DIDENTERFOREGROUND:
                { 
                    hardwareLayer->resumeMusic();
                    whatToDoWithThisEvent = DO_NOT_ADD_TO_QUEUE;
                }
                break;
        }
    }

    return whatToDoWithThisEvent;
}

In my case, I have a wrapper around SDL that I call IHardwareLayer, and it will delegate the calls to SDL as well as do some other useful things.

The above code worked fine. Before I added this event filtering, my music-playing Android app would become unresponsive whenever the app would get suspended. With SDL2 and SDL2_mixer, the music plays on a separate thread, but most of the time you don’t need to know about it. In this case, however, you need to stop the music before your app gets suspended in order to prevent it from trying to play music in a bad state.

Ok, great! But then I updated my libraries in order to handle the ability to load SVGs, and the above code started failing. Except suspending worked just fine. It was the resume functionality that was broken now. How bizarre!

So the symptom is that when the app is suspended, everything is fine, but bringing the app back to the foreground would cause it to become unresponsive.

By adding logs and using adb logcat, I could trace the code all the way until it called Mix_ResumeMusic(), and it never returned from the call. What gives?

After trying a number of different approaches, including updating SDL2_mixer to the latest release, I finally tried to resume the music in the main event loop.
I presume the issue has to do with threading, as I learned I couldn’t call functions that would let me query the pause state of the music in the event filter without the app becoming unresponsive.

The way the event filter works, it gets called before your main event loop, giving you a chance to handle an event beforehand. You can, of course, ignore an event, and in my code above, the default return value ADD_TO_QUEUE is an int that has a value of 1, which has the effect of allowing the event to get handled later in my main event loop when I call SDL_PollEvent().

So I changed the above code so that it no longer tries to resume music and it no longer returns DO_NOT_ADD_TO_QUEUE (otherwise known as the return value of 0) when the filter receives the event SDL_APP_DIDENTERFOREGROUND. Then I added the following code to my main event loop, which is in my HardwareLayer wrapper class:

SDL_Event event;
while (1 == SDL_PollEvent(&event))
{
    switch (event.type)
    {
        case SDL_APP_DIDENTERFOREGROUND:
            {
                resumeMusic(); 
            }
            break;

...

So now I pause the music when handling the SDL_APP_WILLENTERBACKGROUND event in the event filter, and I resume music once my app is up and running the main event loop again. No more crashes or unresponsive app!

I am a bit bothered by the lack of symmetry in terms of how to handle suspending and resuming an app, but software development is often about compromise. B-)

Categories
Marketing/Business Personal Development

Slides from My Presentation at #dsmAgile: “You Are Not a Code Monkey So Stop Acting Like One!”

The relationship between organizations and IT can be quite dysfunctional, and unfortunately they grew up together thinking poor results and demoralizing interactions are perfectly normal.

Developers tend to learn they have no control over their situation, which is unfortunate because they have so much more to offer an organization than merely following orders and writing code to a specification. Some internalize their status of “code monkey” and even take pride in it.

Organizations want efficiency, but then behave in ways that discourage developers from helping them actually become more efficient, and developers behave in ways that give organizations no reason to even think to ask developers for their input.

At dsmAgile 2018, my presentation “You Are Not a Code Monkey So Stop Acting Like One!” seemed well-received, and I had a number of people come up to me afterward to ask if I had been in their organization recently.

You can download the slides here:

Based on conversation I had at the conference, I think I should have focused a bit more on hope for the future and less on itemizing the symptoms and illustrating the problem. My goal was to ask developers and organizations to step up and treat each other as partners, and not to provide ammo for a gripe session. Next time…

What are your thoughts? Do you feel your organization treats you as a partner or as a code monkey?

Categories
Geek / Technical Personal Development

Come See Me Speak at dsmAgile on September 28th

dsmAgile is a one day Agile conference in Des Moines, Iowa, and I’ll be presenting at it this year.

On Friday, September 28, 2018 from 8:30 AM to 4:30 PM, you can discuss, discover, and learn more about Agile software development from people who have been practicing and living it. And it only costs $100, a great deal.

My presentation is called “You’re Not a Code Monkey, So Stop Acting Like One!” You can read more about in the dsmAgile session descriptions, but the main idea is that software developers can and should do more than write code.

You are responsible for not only creating value through technology but also for maintaining that capability, and you don’t get to pretend that “they won’t let me” is a valid excuse.

Merely slinging code is not enough. You are not a code monkey following orders. You are a first-class citizen in the organization, more than capable of driving value as well as delivering it. Let’s discuss how you (and your organization) can start acting like it.

I have enjoyed attending dsmAgile in the last few years, and I look forward to being part it. Will I see you there?

Categories
General

A Goodbye to Diego, My Favorite Pair Partner

At 2:44am, I watched my cat Diego take his final breath. He was about 15 years old, and I knew him for 12 of those years.

Diego

12 years ago, I was living in Chicago and visiting friends in Iowa, and I learned that their neighbor’s daughter was getting married to someone who was deathly allergic to her cats. She was looking for a new home for them, and since I happened to be interested in getting a couple of cats at the time, I took in Diego and his sister Gizmo when they were three years old.

I was told that he was named Diego because he looked like Diego from the movie Ice Age.

Gizmo and Diego

I quickly learned that Diego was a greeter. While his sister hid until she was sure the new people were safe, Diego was always interested in finding out who the new guests were right away.

Greeter

I remember when I was working at a company that made slot machines, and I was in crunch. So, I would leave early, come back late, and I would feel dead inside. I would plop myself onto the couch or a chair, and soon the cats would drape themselves across me. Diego would especially love to curl up with me. Napping together was great.

Me and Diego
Diego and Me
Diego cuddle time
Diego and Me

Even when I was not involved in crunch, whenever I came home, Diego would get up from whatever he was doing, meow frantically, and come see me. I would often pick him up and carry him, and he sometimes put his front paws around me like he was a child being carried by a parent.

I would whistle a certain way, and he often meowed in response, and every so often he would come to me, which was a neat party trick, except he never seemed to want to do it at parties or when other people were present.

Diego is hamming it up

Diego and his sister have been with me through five moves, two states, multiple Ludum Dare competitions, three jobs, and a few relationships. He has been my constant companion for almost a third of my life.

I don’t remember exactly when I realized he was growing old, but it was within the last few years. I might have been tossing him one of his toys and noticing that he was less interested in chasing it. He moved a little slower. But he was still playful, and he was fairly healthy throughout his life. I had to take him to the vet once to get some of his teeth pulled because they were not healthy, but he was fine.

I felt, however, I was on notice, and I realized that one day he might not be around anymore.

In the past couple of months, Gizmo had high blood glucose levels, and so she was on insulin to see if it would help. I changed their diet from dry food to wet food, as I read it can help with reversing diabetes. Gizmo’s levels dropped to the point that she no longer needed insulin, and she eventually bounced back to her old self.

Unfortunately, since I was so focused on Gizmo’s health and her eating habits, I didn’t notice when Diego stopped eating and drinking. They both lost weight, but I chalked it up to a better diet (they were both a bit on the heavy side), but at one point I realized that Diego felt a lot bonier than I could remember. I started to find him hanging out in strange areas, such as the upstairs bathroom, which is never a good sign in cats.

Last week we took him to a vet, and we made an appointment for an ultrasound for this coming Monday, but Diego’s health rapidly declined. It was very quick how he went from seeming perfectly fine to being on his death bed. I checked up on him throughout the day yesterday, but he got so weak that he could barely hold himself up.

My wife and I took shifts to stay with him last night, and when it was my turn, I found he was breathing very shallow breaths. He yowled a bit, and I had no idea if he could see me. I sang to him, talked to him, and pet him. I noticed his paw twitching, and every so often he would take a very large breath, stop breathing for a moment, then pick back up with the shallow breaths. It was awful.

At one point, I saw him stretch his paw out in front of him, which was a lot more movement than I had seen in a long while. A few moments later, he reared his head back, let out a deep groan of a sigh, and he was gone.

I am devastated. I don’t know why his health suddenly got so bad, and I hate that I didn’t see it earlier. I wish he didn’t have to go through as much suffering in his final moments as he did.

Diego, I love you. I will miss the way you climbed onto me whenever I was sitting or lying down. I will miss scratching you under your chin and seeing you close your eyes in contentment. I will miss dancing with you. I will miss your hugs. I will miss how you and Gizmo would curl up to sleep together. I will miss how it feels to come home and have you be one of the first to greet me as I walk in the door. I will miss how you would reach up to my face and pull it towards yours, probably to smell my breath or lick my chin.

I miss you.

My Favorite Pair Partner

Categories
Marketing/Business Personal Development

Don’t Pigeonhole Me: In Defense of the Jack of All Trades

Back when I was going to school at DePaul University in Chicago, the computer science school was introducing a game development minor. To kick it off, they had a panel involving a few people from local game companies such as Midway and Volition, as well as a few members of the Xbox team from Microsoft.

I remember the vice president from Midway Games answering a question from the students in the audience about how to get into the game industry. He referred to some advice that had shown up somewhere from Midway’s HR that essentially said, “Specialize, don’t generalize.”

I remember not liking that advice then, and I still don’t like it now. It sounded to me like “pigeonhole yourself and don’t you dare try to step outside of your hole”. Telling students who are interested in a future career, “You can improve your skills, but only in this one area of life” sounds overly restrictive and a path to misery. Or at least getting let go when your limited expertise is no longer needed.

Two Pigeons

Now, specializing isn’t necessarily bad advice. It’s just not the only valid option, and yet people like to talk as if it is.

I was catching up on my news feeds and saw that artist David M. Kessler wrote If You’re Talking to Everybody, You’re Talking to Nobody.

Don’t be a jack-of-all-trades. Pick what you like to do best, then get really good at it. Try to be better than anybody else. Let people see that you are an expert at the “thing” you do.

When you get known for that one “thing” you’ll see your art career soar. Now get in that pigeonhole and start working on your “thing.”

He says “Any successful marketer and business owner knows that you have to focus your efforts in order to target your market for sales. If you can’t target a market, you won’t make any sales – that’s business 101.”

He’s not wrong about needing a target market, but I don’t think “targeting your market for sales” is synonymous with pigeon-holing yourself into a single specialty.

He listed a bunch of artists as examples to further his point. The idea being that if you think of someone such as Monet or Jackson Pollock, you will inevitably call to mind some representative work of theirs.

But then, what about an artist such as Leonardo da Vinci? What was his “thing”? What about Leon Battista Alberti? Did he have just one thing?

How about someone more contemporary, such as Viggo Mortensen? I knew he was an actor from his role in Lord of the Rings, but I learned he is also an abstract painter and a poet, among other things.

There’s a lot of advice about specializing, and often it is very good advice, but just as often it is also not really supported by any sort of economic principle. Rather it’s more like “common wisdom” that “everyone” knows.

There’s no actual need to pigeonhole yourself into a specialty. Maybe it’s a tougher route to communicate your “thing”, since someone else can’t easily pigeonhole you as an abstract painter, but no one said it had to be easy.

Maybe rather than thinking about arbitrarily self-imposed limits, the more important thing is finding the thread that ties your story together as Pamela Slim argues in her book Body of Work.

Apple makes a lot of different products. Their thing isn’t merely iPhones or Macs. Their “thing” is elegantly, well-designed computing devices. Their “thing” is bigger than what a single product can encompass.

Telling executives at one of the most financially successful businesses in the world “Stop it. You need to niche down” wouldn’t make sense, would it?

The benefits to a focused approach are more than just increased sales. As you begin to focus your work, you’ll begin to get really good at it – better than you thought possible.

While I can see the logic, I also think that I could see someone get really good at their craft if they are able to work with different approaches, techniques, and mediums.

If all you know is oil painting, maybe you’ll get really good at oil painting. But if you know water color as well?

And if you think, “Huh, I wonder how it would look if part of the painting was oil but part was water color?” Would the result potentially be a more interesting work? Would the skill developed in each medium overlap, complement, or reinforce the other?

I don’t know, but I bet a conversation with someone who focused on creating the best work they could with all of their varied skills would be more fascinating and interesting than someone who is Yet Another Oil Painter.

And if their work is more fascinating and interesting, would commercial success also be possible?

According to Vigo Mortensen’s Wikipedia entry, “Mortensen experiments with his poetry and music by mixing the two art forms.”

Do you think his work in one influences the other and makes him a better artist, or does it reduce him to mediocrity because of a lack of focus? How about his music and his film efforts?

Dismissing the jack-of-all-trades as doomed to financial failure easy, but I see no supporting evidence. In fact, in software development, there’s a premium on so-called “full-stack” developers. Rather than hiring someone who only knows how to work with back-end databases or front-end forms, you get yourself a programmer who can do both as well as everything in between.

No one says, “Oh, sorry. we’re looking for a programmer who only knows one kind of programming language.” That’s like saying, “We want programmers who can’t think beyond one paradigm.”

On the other hand, a jack-of-all-trades without a larger purpose, vision, or mission? THAT I can see as being a problem, financially and otherwise.

Categories
Marketing/Business

Sears Needs a Better Purpose, Vision, and Mission

The day I was giving my presentation on the importance a well-defined, compelling purpose, vision, and mission for indie game developers, I read this timely article about a major company struggling with its identity.

Its Survival In Doubt, Sears Struggles To Transform Once Again from Morning Edition on NPR talked about the struggles of the once-giant Sears, which has been operating at a loss for quite some time, despite mergers and selling off brands.

A quote from a customer really sends home the message about how important it is to have a good purpose:

“It fits no niche. It’s not a discount place. It’s not high end,” Mullen says. “It has no identity anymore. I guess it tried to be everything to everybody and it was very successful at that. But now it’s nothing to nobody, which is sad.”

Is your indie game development business similarly raising question marks above the heads of players? Can people tell at a glance what they can expect your games to be about? Do you give them a compelling reason to care about you, or do you “just make games” like most other forgettable indies?

Categories
Game Development Marketing/Business Personal Development

Get Energized and Stay Motivated with Slides from my April 18th IGDA Des Moines Presentation

Most indies pay little attention to their purpose, mission, and vision, but then again, most indies don’t have sustainable businesses. The vast majority don’t make $500 in a year.

Rolling the dice and hoping for a hit, or at least something that earns enough to fund the development of another game, is not a serious strategy.

And there are a lot of new new indie game developers struggling with motivating themselves to work on their projects for more than a few days at a time before the pain of the creative effort overwhelms any enthusiasm they had to be a game developer. There are always posts online asking for tips of staying motivated.

At the most recent IGDA meeting, I presented an updated version of my 2014 talk Playing the Long Game: The Vital Importance of Purpose, Mission, and Vision to Your Indie Game Development Business.

I’m running my business part-time as I have a day job, but doing a poor job of running GBGames as a full-time independent game developer from 2010 to 2012 taught me some major lessons about running a business. Other indie game developers could benefit from my experience.

While there is no video of the presentation, I uploaded the slides with notes in a few formats:

Knowing who you are and what you stand for will go a long way towards reducing the stress and pain and fear that can otherwise be a regular part of running your own indie game development business.

At the very least, it will give you the energy and motivation to keep working on your projects for the long haul.

Categories
Game Development Personal Development

See Me Present at IGDA Des Moines on Tuesday, April 18th

The Des Moines chapter of the International Game Developers Association meets every third Tuesday of the month, usually at the Gravitate offices, a workplace community for entrepreneurs, freelancers and remote workers.

At this Tuesday’s meeting, I’m excited to be presenting an updated version of my 2014 talk Playing the Long Game: The Vital Importance of Purpose, Mission, and Vision to Your Indie Game Development Business, sharing lessons that can be drawn from my experience running an independent game development business full-time between 2010 and 2012, and talking about what I’ve done with my business since then based on those lessons.

Many indie game developers dive into the business head-first with nothing much more than a vague dream and some hope, and often with disastrous results. My aim is to help you clarify your Why, your What, and your How so that your efforts are clear, focused, and more decisive, both immediately and in the long term.

I hope to see you at this free event, whether you’re an established game developer, an aspiring one, or someone who is interested in learning about the behind-the-scenes of games.

You can register for the IGDA Des Moines April meeting, mainly to ensure we’ll have enough pizza and drinks for everyone. B-)

Categories
Games

Limiting Screen Time for Your Kids Isn’t Necessary?

When I was younger, my parents would tell me to turn off my Atari 2600 or my Nintendo because I was staring at the TV for too long. I remember one time in particular in which my father said something to the effect that it would ruin my eyes to play for so many hours at a time. I subconsciously rubbed my eye at that point, and he said, “Ah, hah! See?”

And I have felt self-conscious about rubbing my eyes after long sessions in front of the computer ever since.

But the main point is that I have always had this internalized idea that too much time playing video games or watching TV is bad (although it didn’t stop me from playing Civilization all night once…ok, a few times…I can stop taking turns anytime I want to!). There were health reasons, and there was also the idea that I should get outside into the fresh air more, or be more social.

As a game developer who is interested in creating entertainment that encourages curiosity, supports creativity, and promotes continuous learning, I would love to be able to watch my niece play the games I make and get not only real-time feedback but also help her on her journey to becoming a terrific person.

But when I visit, I find myself wondering if perhaps I shouldn’t contribute to even more of her screen time, as I almost invariably find her playing video games either on a tablet or on the computer.

If anything, my family is often getting her away from the computer to interact with people in the real world before she forgets how to do so. People like me, for instance. “Hey! I live over 300 miles away and only visit for a couple of days every few months. You could at least look at me once or twice!”

I know some people who use screen time as a reward for doing chores or good behavior, and taking away screen time is a punishment. By and large, their kids are not allowed to play games or otherwise use computers recreationally for more than so many hours per week.

But according to Christopher Ferguson of Stetson University, a recent study he led found that “there is only a negligibly small association between excessive screen time and higher levels of depression and delinquency among teenagers.”

Well, that’s good news. Playing too much Minecraft or Halo isn’t what leads your kids to becoming disaffected youth. Whew!

What’s more, Ferguson argues that since computers are so integral to society and how we live and work, preventing children from become familiar with modern technology is likely to prevent them from being able to participate in our increasingly fast-paced lives, which is the exact opposite of the result many parents might want when they limit screen time.

Funny, I’m pretty sure that was the argument I used to convince my parents to get me Mario Paint, which came with a mouse peripheral.

Naturally, our focus can shift from how long children play games to what games they are actually playing. I’m reminded of Steven Johnson’s Everything Bad is Good for You, which argued that today’s TV and video games are actually more challenging than they were decades ago, and so children watching TV and playing games today are essentially training themselves in decision-making and other important skills.

Even if too much screen time isn’t a real issue, I still might pry my niece away from games periodically, if only to be able to catch up and spend quality time together away from the screens. But the Ferguson’s study made me feel more comfortable letting my niece play games that teach responsibility and strategic thinking such as Toytles: Leaf Raking.

Of course, I also think a well-rounded video game education is in order, starting with the classics.

Let me dig out my Atari 2600…

Categories
Game Development Geek / Technical

Book Review: Behavioral Mathematics for Game AI by Dave Mark

Behavioral Mathematics for Game AI Book

Intrinsic Algorithm’s Dave Mark, a fixture at the Game Developer Conference’s AI Summit, is also the author of Behavioral Mathematics for Game AI.

Most game AI literature covers the basics in a general way, such as finite state machines, flocking and steering to control movement, pathfinding algorithms such as Djikstra’s or A*, goal-oriented action planning (or GOAP), and more.

Mark’s book, however, covers a specific topic in great depth: game AI decision-making.

You might have a character that can do interesting things such as hunt, flee, eat, track, alert nearby allies, etc, but if you don’t create a good system that allows that character to make decisions between those behaviors, it may not convince your players that it is intelligent at all.

The goal is to create behavioral algorithms to get computer-controlled agents responding to their environment in believable and sensible ways. To get there involves a journey through the subjects of psychology, decision theory, utilitarian philosophy, and probability and statistics, among others.

Mark was great at walking you through each step of this journey, combining theory with detailed explanations and examples, including code. Sometimes I felt the detailed explanations were a bit too detailed, but at no point did I feel like I was lost.

He never made a leap in logic that left me behind because he was holding my hand at every step of the way. Sometimes I appreciated that hand-holding, especially for the more involved statistics, but there were a couple of times when I found myself getting a bit impatient and wanting to run ahead.

And it is probably partly due to the fact that it’s a long journey. At one point, I realized I was over 300 pages into the book without feeling like I knew how to integrate and apply all of the individual tools I was learning into a cohesive system.

The examples he used to illustrate his point were sometimes bizarrely relatable. I have never tried to create a model of my behavior related to when I decide to replace my older razor blades with newer ones, but Mark did, and I actually found myself nodding with recognition that I do tend to use my last blade in the refill pack for way longer than my other blades.

Other examples demonstrate how his utility-based decision-making system can address problems with past games, such as the strategy game AI that kept sending its attack force towards the most vulnerable target. Savvy players can keep defensive forces outside of city walls, then place them in the city at the last moment and moving units out of a city far away. By doing so, they could keep the AI units moving back and forth, unable to carry out an attack, for as long as they want.

Solving this issue involves giving the AI the ability to have decision momentum by making decisions include all of the relevant information. The AI isn’t just deciding what city to attack. A single decision is what city to move to AND attack, which incorporates the time it takes to travel to a target city. Suddenly, the decision to change course in order to attack a different city is a bit more painful, and so the current target is more likely to be maintained.

I appreciated that he covered the problem of having the AI always making the best decision. While academic researchers might love that result, players are likely to find such AI as unrealistic and, worse, uninteresting. And if there are multiple AI agents that do exactly the same thing simultaneously, it’s even more of a problem. So there’s an entire chapter on ways to ensure that the game AI can be reasonable yet still interesting from one play session to the next.

The book was published in 2009, and so you would think it means that any information you could glean out of it would be obsolete after almost 10 years of advances and progress in the field. And yet, the basic decision-making system that drives behaviors is still relevant.

One of the benefits of reading an older book is seeing the ideas of that book illustrated in front of you in other media.

You can see Mark’s talks with Kevin Dill from GDC 2010 and 2012 in the GDC Vault. Improving AI Decision Modeling through Utility Theory and Embracing the Dark Art of Mathematical Modeling in AI both introduce the use of this utility-based system in games.

In 2013, Mark’s portion of the panel Architecture Tricks: Managing Behaviors in Time, Space, and Depth introduced the Infinite Axis Utility System, which takes the concepts from the book and puts them together into a simple yet powerful architecture.

In 2015, Mark and Mike Lewis presented Building a Better Centaur: AI at Massive Scale, in which they describe the Infinite Axis Utility System that was the architecture behind an MMO.

I’ve seen these videos before, but having now read the book, I found that upon rewatching them that I understand the sections on response curves and how they apply to the actions the IAUS chooses.

Behavioral Mathematics for Game AI is not a beginner’s book at all, but if you are interested in learning how to give your AI powerful reasoning abilities that produce rich, believable behaviors for your players and want it to be easy to understand and design with, I’m not aware of another book on the subject that is as accessible as this one.