Month: January 2021

Thoughts about the Onslaught map generator

Thoughts about the Onslaught map generator

Map
Image by DarkmoonArt_de from Pixabay

With the 2nd Tutorial under my belt, I’ve ben thinking about the map generator which is the subject of the third tutorial. I suspect it will be based on the Empire map generator which you can read about from an earlier blog post.

The difference is that there will only be one continent not several and it has to occupy between maybe 50% and 80% of the specified map size. These are 20 across by 15 deep (small), 30 x 20 across (medium) and 40 across x 30 deep (large). On a 1024 x 768 Window, I managed to fill it with 31 across by 28 deep, so I’ll need a larger playing Window.  The current one is 1024 x 768.

The Empire generator works by first scattering a number of land and sea points in an empty grid then growing layers around each point in the empty cells, stopping when a non-empty cell is found.  I’ll need to modify it so that the starting land points are near enough to form a single continent when grown and reject any map that has 2 or more continents.

I also put Onslaught on my Raspberry Pi and found that it runs a lot slower than on the Hyper-V Ubuntu. I can only put this down to the number of hexagons. a screenful at 1024 x 768 is 31 x 28 hexagons and that requires 868 blits ( each a call to SDL_RenderCopy). Each blit copies 1088 bytes in VRAM.  That’s almost 1 MB of VRAM being copied in itself each frame which is not that much for a GPU but there’s probably some overhead for each blit and I imagine that could be the performance killer.

Kilo- a thousand lines text editor in C

Kilo- a thousand lines text editor in C

Antirez kilo text editorDeveloped by Salvatore Sanfilippo aka antirez and licensed under the BSD 2 licence, kilo is a simple text editor in one file.

If you are learning C and want to see how to write a utility, this might be a good example to follow. Warning though he does use pointers so make sure you’ve learnt them first!

I had a stab at writing one quite a few years ago but it wasn’t very good. I have a suspicion that writing a good text editor depends upon you first creating a good implementation of the text storage. Solve that and it’s downhill for the rest.

I’ve added this to my curated library of C code, on the C Code link on the top menu.

 

Onslaught (aka Slay on Linux) tutorial two published

Onslaught (aka Slay on Linux) tutorial two published

Onslaught linux hexagons screenI’m quite pleased with this. It took about six hours in total to create including the time to create the graphics. Running in Hyper-V under Ubuntu 20.04, it draws a screenful of graphics in about 65 microseconds.

I took the hexagon drawing code from the AboutEmpire.zip code on GitHub and modernised it for SDL2. The Empire code uses Surfaces from SDL1 while this uses Textures from SDL2.

Orange hexagon Blue hexagon There are nine hexagons with all but the dark one having an internal border.

I think the orange and salmon hex look a bit too close, so I’ll change one of them.

The tutorial goes into a bit more depth. about the program (which is just over 200 lines long) and can be found on GitHub in the file Onslaught1.zip.

lldb-mi no longer exists

lldb-mi no longer exists

lldb vs code extensionsThis is debugging vs code with lldb. It seems as of clang-10, it’s no longer supported which is a pita. I’d been working on the Onslaught code and something wasn’t working so I thought I’d try to debug it.  However the debugger wouldn’t start. Investigating a bit further and I found this discussion thread on the GitHub cpp tools.

I have to say this. I find Linux development quite frustrating at times because of things like this. Something that worked in an earlier version of clang has been removed. Why? There’s a similar issue with Ubuntu 18.04 compared to 20.04 running in Hyper-V VM. With 18.04 I can use the clipboard to copy and paste between Windows and Ubuntu, but so far I have been unable to do it in 20.04.

You spend ages wasting time trying to find answers. I find Windows development with C# much less problematic. Open source developers sometimes don’t appear to take the needs of their users as seriously as say commercial developers do.

So now I’m looking a for a better way to debug C/C++ programs from vs code. There is a lldb – vs code adapter as well as native debug and Code LLDB (see screenshots) so I’ll investigate and see if any of these work. Or I can just study my code and work out in my head why it isn’t working!

Interesting article on the state of Linux gaming

Interesting article on the state of Linux gaming

Linux playYes it’s on Medium, but its worth a read. The gist of the article and this one is saying that there’s a problem with Linux for gaming. Also that anyone new to Linux gaming is looking online and finding old articles that suggests that SteamOS is a good distro to use while in reality it hasn’t been updated in a while.  The problem is that Valve (creators of Steam) have let SteamOS languish.

Though its not as if Valve have ignored Linux gaming. They released an open source tool Steam Proton (Link goes to it on GitHub) that lets you run many Windows games (6000 or so)  on Linux. The Linux Steam client includes a copy of Proton.

The problem though is that this hasn’t really helped attract more people to Linux gaming. People I know either play games on Windows or on consoles like PlayStations.  Linux is perceived, wrongly I’m sure as being an inferior game platform.

Doing 3D Math(s) in C

Doing 3D Math(s) in C

Donuts.cIf you’re interested in seeing how 3D math(s) are done, this article by ex Googler Andy Sloane is about a program he wrote called donut.c. The name is doubly appropriate as not only is the source code shaped like a Donut (doughnut for us Brits), when run on Linux, it outputs an animated 3D donut. This was an attempt at obfuscated C back in 2006.

Now 15 years later he’s updated it to work without using the math library. Interesting is the explanation of how he did all the mathematics without using sin and cos directly. Also it includes a version that uses 10 bit integer arithmetic; no floats at all. If you’re interested in understanding how Donuts.c works, read this post from 2011.

I like articles like this- they’re a bit different and add to programming knowledge. and you can never get enough of that

 

 

 

Interesting Visualisation – like tixy.land

Interesting Visualisation – like tixy.land

Tileflip visualisationIf you remember tixy.land that I mentioned back in November last year, here is another website that lets you control a visualisation. In this case, tileflip.xyz lets you create a single function and applies it to every square on a board. You can control how many squares the board has and how often they are updated through a pair of sliders. You can pause the animation and toggle squares by clicking on them in the mouse. One of the early examples runs Conway’s Life and has a really short function.

The functions can be quite complicated (it’s JavaScript) as this example shows which moves a ball around the screen while changing colours.

The ctx (short for context) object has various properties and functions that you can call on it. For example ctx.resolution is how many squares there are on the board and this page lists all the properties and functions.

function rule(ctx){
  
  ctx.color = ctx.floatToColor(10/ctx.t)
  
  let r = Math.min(ctx.t**2/10, ctx.resolution/4);
  let cx = ctx.t*2%(2*ctx.resolution+1);
  let cy = ctx.t*3%(2*ctx.resolution+1);

  
  if(cx >= ctx.resolution+1){
    cx = 2*ctx.resolution - cx;
  }
  
  if(cy >= ctx.resolution){
    cy = 2*ctx.resolution - cy;
  }
  
  cx = cx/(ctx.resolution)*(ctx.resolution-2*r+1)
  cx += r - 1;
  
  cy = cy/(ctx.resolution)*(ctx.resolution-2*r+1)
  cy += r - 1;
  
  let toReturn = inCircle(cx, cy, ctx.x, ctx.y, r);
  
  if(!toReturn){
    ctx.color = 'black'
    ctx.invert = true;
  }
  
  return toReturn;
}

function inCircle(cx, cy, x, y, r){
  return (cx-x)**2 + (cy-y)**2 < r**2 ? 1 : 0
}

JavaScript is like C in many ways and while not the main purpose of LearnCGames.com, websites like this are an inspiration to games creators. My other interest is C# and I’m currently learning about Blazor a technology that lets you create webpages in C# using WebAssembly so it’s quite possible that things like this could be done in Blazor.

But it’s fun to look at the different example functions and you can also download the images as gifs via a link at the bottom of the page,

New Tutorial – Implementing the Windows game Slay

New Tutorial – Implementing the Windows game Slay

Slay gameOne of my favourite casual games is Slay by Sean O’Connor. I bought the full game which sells for $10 and it’s still selling well via his site or Steam. His game is only for Windows so my reimagining (great word!) will be for Linux only including Raspberry Pi.

His game comes with several hundred islands, in four sizes from very small, small, Large and very large. You play against 6 computer players with intelligence ranging from very stupid, stupid, clever to very clever. In addition there are a bunch of predefined maps like Britain or even the world.

Troops exist in four sizes with each size three times bigger than the one below. Your territory must be big enough to support the troops there and if it isn’t they all starve.  Splitting an enemies’ territory to starve his troops is a valid tactic.

Add in with trees which can spread and waste land which spreads even faster and its a cute addictive game.

My game will be called Onslaught and use its own graphics. Islands will be computer generated and hopefully the AI will play a decent game. The first tutorial which just explains the game and the scope of what I intend to do is now available.

 

 

Atoms tutorial three- finishing the game off

Atoms tutorial three- finishing the game off

Atoms 3 screenshotI fixed the bug in the Atoms game and the third tutorial finishes it off. The source code (in this case atoms3.c) has been placed on GitHub and the final version is about 300 lines long.

As well as the bug fix, I added a line to show the number of cells owned by the player and the computer. You can see it after the player move (Player : 41 Computer 16) and after the Computer move.

This whole atoms game was an experiment to see if I can introduce C programming without having to go overboard explaining every statement etc as I’ve done in the past. I think I’ll get back to programming games!

 

Tronus Project 1945 – Coursework

Tronus Project 1945 – Coursework

Project 1945A university student by the pen name of Tronus has put a project from his 3rd year in his degree course on GitHub. The screenshot shows the game running on my PC. The build folder includes a release folder with game exe plus all dlls and game assets in a resources folder below that so you can run it immediately on Windows. ,

I vaguely remember playing a similar game (a vertical scroller) in arcades back in the 1980s. That game was 1942.

The 1945 game has been programmed in C and uses SDL2. As always I took a walk through the source just to compare how it was done say compared to my own efforts. One thing I noticed is that he uses a fixed delay of 11 milliseconds each frame in the game loop by calling SDL_delay(11) rather than setting up the video to sync to the fly back. Is this better or worse? I’m not sure.

I think I prefer my method in case something takes a bit longer. Having a fixed delay means that you could risk overrunning the 16.666 milliseconds allowed per frame. That could never happen with my asteroids game as syncing to the fly back means my game can take as long as 16.66 milliseconds per frame and still not overrun.