Posts

Showing posts from September, 2024

Deep C.

In this thread I will post interesting features and usages (of mostly the C programming language), that might not be known to a novice programmer. I might figure out how to display code nicely, at some other time.  typedef (declare) a struct name before defining said struct: typedef struct myStruct myStruct; struct myStruct {      myStruct* next      ... };  You can also typedef (declare) a pointer to a struct without defining the struct to achieve type safety (the pointer cannot point to a struct with a different type), and encapsulation (because it doesn't have access to the definition of said struct). Constant on the left!   if(NULL == x){...} is better than if(x == NULL){...} because then you don't get if(x = NULL){...} by mistake Declare type once: struct T *p; p = malloc(sizeof *p); instead of: struct T *p; p = malloc(sizeof (struct T)); Similarly sizeof array = (sizeof (ary) / sizeof (ary[0])) Single item in list or not in any list?

Maths Game.

Image
 I am (occasionally) working on an action RPG game where every time you attack you have to answer a maths question. RPGs are cool and I used to play them a lot but now I am a social gamer and I don't appreciate the brain-drain. I have accrued a large repository of spaghetti code with a few gems as I learn the ins and outs of game programming in C ( https://github.com/BostonBrooks/Spaghetti_Graveyard. ) . I chose C because it is what the electronic and computer engineers use and working as an ECE is a long term goal for me. I am using the C binding for SFML because SDL doesn't easily incorporate 2D meshes, to be used for 2.5D curved ground surfaces. A few demos below. Scritchyness is due to screen capture software.     All for now, Boston.   Update 17/02/2022 In the game, the terrain is represented as a 2 dimensional grid where each point has a third dimension (height). I have used bicubic interpolation (functions with x^3 and y^3 terms) to smooth the areas between points on the