Simple C question

Simple C question

question marks
Image by Gerd Altmann from Pixabay

I saw this this morning and got the correct answer. I also tried it just to be sure in Visual Studio and apart from a warning, it compiled ok and ran and gave the correct answer.

#include <stdio.h>
int main() {
	float* p = (float*)50;
	p = p + 3;
	printf("%u\n", p);
}

If you want to avoid the warning, change the last line to

	printf("%u\n", (unsigned int)p);

So the question is what does this output?

  1. 53
  2. 62
  3. 66
  4. 68

Answer and explanation tomorrow.

Next atoms tutorial added

Next atoms tutorial added

Atom2sThe first version of the program was 90 lines long but now it has grown to 275. This includes code to let the computer play and checking code plus I’ve refactored it a bit, simplifying the code.  I haven’t extensively tested it and at least one bug has crept in. Occasionally the computer seems to claim ownership of a player cell. This stops you adding one to that cell so its not good.

You are welcome to have a look at the code and fix it. I will in a few days. The code is on GitHub and is a single file atoms2.c. As before its been compiled with Visual Studio 2019 Community edition though it should compile more or less unchanged with gcc, clang etc. It runs in a terminal/command line. You can see the computer playing in the screenshot.

I’ve also added the tutorial for this.

Where are the business games?

Where are the business games?

Entrepreneur image
Image by Gerd Altmann from Pixabay

Back in the 8-bit days there used to be a few business simulation type games about. These were mostly text games; I remember one was a multiplayer game about making and selling televisions; you had to make decisions about how many lines to run in your Factory, how much to spend on marketing, R & D etc. and then each turn the program would determine how many TVs you had sold and whether you made a profit or loss.

The only modern equivalents seems to be games like “Pizza Tycoon” and of course the web/mobile game FarmVille (which has just shut down!) . There are others e.g. Airport Tycoon etc. But the simpler games don’t seem to exist any more.

The type of game I was thinking about were more like business simulations. Back before home computers were popular in the late 1970s, I worked one summer on a farm owned by a bloke who created business games for companies for use as training aids.

One such game I played ran in a games ‘Zine back in the late 70s. The person running the game must have had a computer because each turn I’d receive a complicated printout with all sorts of accounting information on it. Things like Cost of Sales, depreciation of value of stock, cost of storing stock and so on as well as P & L and Balance Sheet. It had to have been written by an accountant!

At the very simplest there are games like Hammurabi where you have to survive multiple years by selling land, planting and harvesting wheat to feed your population. This page on Wikipedia lists nearly 200 commercial and web games. It doesn’t however mention the likes of Dope Wars or even torn.com. Possibly those are perceived more like rpg/mmorpg rather than say pure business simulations.

For the simpler type of games like the ones I was describing, C is not a bad language to implement them in.

 

 

Program a NES game in C

Program a NES game in C

BNES Marioack in the mid 80s I was busy writing games for ZX Spectrum, MSX and CBM-64 and also porting games between 6502 and Z80. That was also when the original NES appeared.  That had a 6502 CPU but a lot less RAM than the CBM-64.

In those days the NES was programmed in assembly language. C was still too new for most things but now, it’s possible to program a NES game in C. Developer Douglas Fraker has produced a series of tutorials on how to create NES games in C using cc65, a C compiler/assembler that takes C code and outputs 6502 assembly language.

His tutorials cover the whole range of things including sprites, how palettes work, music, sound effects, even how to use the Zapper (a stand alone gun-like accessory) with all source code on GitHub.  You can run games on an emulator such as the one that comes with RetroPie.

Reading the page on 6502 ASM Basics brought it all back to me even though it’s 31 years since I last wrote 6502 code.

 

.

Fascinating online WebGL

Fascinating online WebGL

WebGL RabbitsWebGL (Web Graphics Library) is a JavaScript API for rendering interactive 2D and 3D graphics in a compatible web browser without the use of plug-ins. A developer called Todd Fleming has created a webpage where C++ programs (and maybe C?) can be compiled online by Clang and then run in the browser.

The colourful rabbits (numbering approximately 30) in the screenshot are rendered in real-time and rotated and moved (transformed). Each rabbit is actually each made up of 66,848 triangles.  Just click the (WebGL- Flying bunnies)  link on the right-hand side to load the 335 lines C++ source program then hit the compile button. After it has compiled in a second or so hit the Reboot/Run button on the right-hand side to start it running.

You can select all the source code and copy/paste it into a text editor if you want to examine it. You even can save out the compiled wasm (WebAssembly) file if you really want to though as it’s binary, it probably won’t mean too much unless you have a viewer.

 

 

Interesting notes on C

Interesting notes on C

Notes
Image by David Schwarzenberg from Pixabay

If you are learning C there are a number of points that may not be mentioned and possibly aren’t obvious. For instance with multidimension arrays. “The array size can be derived from its initialization but that’s applicable for first dimension only. For example, ‘int arr[][2] = {1,2,3,4}’ is valid but ‘int arr[2][] = {1,2,3,4}’ is not valid.” and “Unlike ‘long long int’, there’s nothing like ‘long long double’ in C. Besides, real data types (i.e. floatdoublelong double) can’t be unsigned.”

These and a whole lot more notes can be found here. It’s definitely worth a read. The only thought I had that was worthwhile is to do with the order of execution in a for statement.

The definition is in this order: for (initialisation, end test, alter loop variable ) statement;  but the actual order of execution is initialisation; statement; alter loop variable; end test.

Is there a market for games on Linux?

Is there a market for games on Linux?

Ubuntu GamesDon’t get me wrong, I like Linux. I’ve put in enough hours, especially on Raspberry Pi programming but games don’t seem to sell at all well on Linux. For one there’s a massive expectation of free software. Ubuntu comes with plenty of free games and some really good quality ones like 0Ad or Battle of Wesnoth.

The screenshot is from the Games category of the Ubuntu software installer. A very rough count suggests there’s over 420 games available there. Add to that emulators and ROMs (if you can get them) and you end up with almost 20,000 games.

I bought a Retro Pi at a Winter fair a couple of years ago and it came with 18,000 games. It was an Orange PI with a 16GB SD Card running the Retro Pie distribution which is available online. There’s no way the games are legal BTW, I even found four games that I wrote back in the mid 1980s running on the ZX Spectrum and CBM-64 emulators. All of the four games were smaller in size than the screenshot image! (Please don’t ask for a copy of the games. I gave them away with the Orange Pi to a nephew.)

If you are a Linux developer, how many games have you ever bought?

As this article on The Register explains “In the past year, Puppygames has sold 290 of its eight games on Linux and nearly 9,000 on Windows. ”

Most money I’d guess is made by companies producing AAA games especially on console.

So if you restrict yourself to developing games only for the Linux market then I imagine it would be very disheartening. However it’s not difficult to port them to Windows. Or you can look beyond desktop and venture into mobile development and web games. Both are considerably different to desktop games development and have their own problems (like getting your mobile app found when there’s a million others competing against it!). Or you can go down the “Write an ebook book about game development” as I have done.

Wizznic – a cross platform game in C

Wizznic – a cross platform game in C

Wizznic screenshotWizznic is a sliding blocks puzzle game. The game is written in C and used SDL and SDL Gfx. The developer a Danish programmer called Jimmy Christensen has made it freely available on GitHub so you can modify it, do what you want with it. It’s distributed under the GNU GPL (General Public Licence).

The game itself is multi level and there are other levels (DLC – free) available, just get the code on the content page. The game includes an editor which is why I guess there are so many downloads available.

The game itself is for gp2x, Windows and Linux and also includes make files for Pandora and PSP (PlayStation Portable). I’m always curious about the source code and the main game comes in 36 .c files and 37 .h with the biggest file being the 2000 lines of code menu.c.

If you are learning C or just want to see how a game is done, it’s an excellent way to study, especially because it’s cross platform. I suspect it’s SDL 1.25 because it uses surfaces rather than textures as in SDL 2.

 

Tutorial on Atoms added

Tutorial on Atoms added

Atoms command lineThis is my alternative approach to learning C. Show code, explain what it does in depth rather than explaining the C language feature-by-feature. I’ve created a 90 line skeleton program for atoms. The full file is on GitHub, The file is called atoms.c.

All this does is setup the board and let you enter your move. I’ve tested it on Windows and it calls _getch() to read the keyboard so it’s fast to enter moves and you don’t have to press enter/return. It then validates the move and updates the board and draws it. No chain reactions yet or computer play but those will be in future tutorials.

In the screenshot you can see I added one atom to (3,3) and then (5,6).

The board uses three characters for each cell with atoms prefixed by P or C for player and computer.  Trying to have the computer play well will be an interesting bit of programming.

 

Raylib – a C library for videogames

Raylib – a C library for videogames

Raylib screenshotaMuch as I love SDL, I’m quite happy to look at alternatives and Raylib looks very good. It was inspired by Borland BGI graphics lib and by XNA framework (both of which I’ve used).

It’s written in C99 and is very cross-platform, even listing Raspberry Pi (I’m guessing they mean Raspberry Pi OS) as well as Windows, Mac, Linux, Android and HTML5. Basically any platform that supports C and OpenGL. There is a massive list of bindings for use in other programming languages.

Check out the examples page; you can try them online and each has a C source code listing so you can see how it’s done.

What I really like about RayLib is the scope of it and this is where I think it beats SDL. There’s 2D and 3D support, text support, in eight modules (core, shapes, textures, text, models, shaders, raudio and physac). It also comes with 8 free fonts but you can use your own custom fonts, pretty much the same way as I did in the Asteroids game.

I was very impressed with the text demo where a red box contains the text and if you resize it with the mouse the text is word wrapped.

The Spanish developer Ray (surname unknown) was cited today as the #1 trending C developer on GitHub and that’s a testament to the seven years that he has put into Raylib. His design philosophy with Raylib is to make programming fun and I am going to check it out.  If it looks as good as I think it is, I will be switching to it for future games work. One thing that particular intrigues me is the RayGui library– an immediate mode GUI library. There are various tools that have been designed with it. and it supports these GUI controls:

Label       |  Button      |  LabelButton |  ImageButton  |  Toggle      |  ToggleGroup |  CheckBox
ComboBox    |  DropdownBox |  TextBox     |  TextBoxMulti |  ValueBox    |  Spinner     |  Slider   
SliderBar   |  ProgressBar |  StatusBar   |  ScrollBar    |  ScrollPanel |  DummyRec    |  Grid

RayGui table