Homebrew WIP Brick Game 9999-in-1 Clone Project

The Real Jdbye

*is birb*
Member
Joined
Mar 17, 2010
Messages
23,288
Trophies
4
Location
Space
XP
13,846
Country
Norway
Dev Log Experimental - Too Many Graphics!

Programming natively for the Switch in C or C++ is a relatively low level operation. Unlike when working with a studio (or even as an indie), you don't have the luxuries of using libraries and dev environments built with millions of dollars like Unity and Unreal. For the most part, you have to rely on free, open source, community made projects, which are great and I'm thankful for, but robustness and especially documentation tend to suffer.

I'm open to the fact that everything I'm about to describe having trouble with has a simple solution, but that lends to my point since I read basically all the documentation available and through countless forum topics discussing it. (If you know the answer, let me know, please!)

For this project, I'm using a port of nanovg, a handy shape and graphic drawing lib, on top of deko3d, the raw screen drawing calls.

What's the problem?
At a certain number of draw calls, about 500, the app will crash with an out of memory error. This counts for everything. Every sprite, letter of text, colored rectangle, etc. As you can imagine, with the 20x20 grid of sprites plus a few lines of info text on the screen and we're already out of slots.

cell_unselected.png
/
cell_selected.png


What's my solution?
Instead of drawing every plip in the grid, I decided that it might be a good idea to instead draw 2x2 plip graphics. (We can do larger sizes but that's for later if needed.)

cells_00.png
...
cells_01.png
...
cells_13.png
...
cells_15.png

( 4 out of 16 combinations )

But how do we do this the best way?

----------------------------

If we know that 0 is off and 1 is on, we can map things out like this:

Binary 0 - Decimal 0
cell_unselected.png


Binary 1 - Decimal 1
cell_selected.png

Binary 00 - Decimal 0
cell_unselected.png
cell_unselected.png


Binary 01 - Decimal 1
cell_unselected.png
cell_selected.png


Binary 10 - Decimal 2
cell_selected.png
cell_unselected.png


Binary 11 - Decimal 3
cell_selected.png
cell_selected.png

Binary 0000 - Decimal 0
00
00
cells_00.png


Binary 0001 - Decimal 1
00
01
cells_08.png


Binary 0010 - Decimal 2
00
10
cells_04.png


Binary 0011 - Decimal 3
00
11
cells_12.png


Binary 0100 - Decimal 4
01
00
cells_02.png


Binary 0101 - Decimal 5
01
01
cells_10.png


Binary 0110 - Decimal 6
01
10
cells_06.png


Binary 0111 - Decimal 7
01
11
cells_14.png


Binary 1000 - Decimal 8
10
00
cells_01.png


Binary 1001 - Decimal 9
10
01
cells_09.png


Binary 1010 - Decimal 10
10
10
cells_05.png


Binary 1011 - Decimal 11
10
11
cells_13.png


Binary 1100 - Decimal 12
11
00
cells_03.png


Binary 1101 - Decimal 13
11
01
cells_11.png


Binary 1110 - Decimal 14
11
10
cells_07.png


Binary 1111 - Decimal 15
11
11
cells_15.png

----------------------------

Great! So how do we draw this?

Now that we've mapped out which graphic is which above, we can easily to refer to each, assuming they're named based on the decimal (for example, cells_00.png, cells_01.png, ..., cells_15.png),

Let's say that we have the boolean true/false for each of the 4 locations stored to the variables:
top_left : TL
top_right : TR
bottom_left : BL
bottom_right. : BR

The slow, boring, sad way to do this is to just write out every single possibility.
Code:
// none active
if (!TL and !TR and !BL and !BR)
    draw_sprite("cells_00.png");

// just bottom right
if (!TL and !TR and !BL and BR)
    draw_sprite("cells_01.png");

// ... now repeat this all 16 times lol

// all filled
if (TL and TR and BL and BR)
    draw_sprite("cells_15.png");

As you can see, this is so much work that I didn't even want to write them all out for this example!

For 2x2, that's 2^4, 16 options.
If we wanted to draw a 3x3 grid, that would be 2^9, 512 options.
4x4? 2^16, you'd have to write 65,536 if statements.

Surely there must be a better way. Maybe a way that only takes a couple lines no matter how big the grid.

Bitwise tricks!!
Let's talk about how binary numbers are represented.

Each position is a power of two, laid out something like this:

0000 <- Binary positions
8421 <- Value of each position

Based on that, just add up the values of the activated positions.

0000 =
(0*8)+(0*4)+(0*2)+(0*1) =
0 + 0 + 0 + 0 = 0

0101 =
(0*8)+(1*4)+(0*2)+(1*1) =
0 + 4 + 0 + 1 = 5

1111 =
(1*8)+(1*4)+(1*2)+(1*1) =
8 + 4 + 2 + 1 = 15

The trick now is to somehow encode the four plip statuses into this format!

As we know from my chart above, each position in the binary could be compared to each of the plips. This next part is where I might lose some of you, but I'm not quite sure (while writing this at 5 am) how to bridge the gap any smoother.

If we want to convert a 0/1 false/true relationship into the four places, we can do something like this:

sprite_num = (TOP_LEFT * 8) + (TOP_RIGHT * 4) + (BOTTOM_LEFT * 2) + (BOTTOM_RIGHT * 1)

and the resulting number will be a number from 0 to 15, corresponding to the correct sprite and it's then as simple as:

draw_sprite("cells_" + string(sprite_num) + ".png");

And that's it! No matter how big our grid gets, using those two lines of code, we can retrieve the right graphic, instead of needing exponentially more if statements for each size upgrade.

There is a more in-depth way to explain why to multiply those numbers and how to shift bits, but I'm trying to find a place between overly technical and not technical enough with this post. If any of it isn't clear or you have any questions and would like to learn more, feel free to ask! And if you liked this write-up, let me know and I'll see if I can do more in the future for interesting topics. :)

As this project is open source on my github, you can find the code in question right here! Just a warning, though, since this is early in turbulent development, the linked code may be rewritten or moved somewhere else sometime soon! (Also, in my implementation I reversed the order of the bits because it was a little prettier to me, but the explained method is the standard order.)

Thanks!
You can go further and use 4x2 or 2x4 blocks. That way each block fits neatly into a byte of data, making the memory data easy to deal with without a single bit wasted.
 

Chrscool8

Well-Known Member
OP
Member
Joined
Oct 23, 2008
Messages
114
Trophies
1
XP
934
Country
United States
@Chrscool8 if i remember well , i have a suggestion ;
i wonder if in the original game , when you hold the desired directions buttons or by pressing A , the snake go very fast. ( even in the first second play).

i was remembering playing tetris/snake/race car ,like this.

thank you again for this nostalgic HB , kids are happy playing it ;)
Funnily enough, I didn't remember that, but planned to add it anyway, independently! Expect to see that shortly. Glad you're enjoying it! Watch for more minigames soon!

You can go further and use 4x2 or 2x4 blocks. That way each block fits neatly into a byte of data, making the memory data easy to deal with without a single bit wasted.

Good stinkin! Thankfully I don't have to optimize that hard yet, but that would definitely be a nice little thing to do in the polishing stages.

----

Edit:
Btw, just dropped an update with Pong and an AI opponent. :)

----

Edit again:
Added a fast mode when you hold a trigger to up the ante!
 
Last edited by Chrscool8,

Chrscool8

Well-Known Member
OP
Member
Joined
Oct 23, 2008
Messages
114
Trophies
1
XP
934
Country
United States
Hey, guys! Enjoying Pokemon Snap? Dropping in to update with a preview of what I slapped together today!



Remember this one? As we've now hit game 4, we've reached the point that I've been putting off for a while, which is a proper menu! Now that I've run out of face buttons, I should probably get to it. Have any suggestions on how to implement it elegantly? Are you okay with the old style A, B, C, etc, selection panels? I wanted to have options menus for each games to customize/remix them, too. Not sure how to get that all in there.

Also, what games would you like to see next? Maybe you even have an idea for a new game? I'll do my best to add what I can!
 

mathew77

Lovin' life.
Member
Joined
Jan 19, 2019
Messages
1,186
Trophies
0
Age
47
XP
3,676
Country
Kazakhstan
Also, what games would you like to see next? Maybe you even have an idea for a new game? I'll do my best to add what I can!
That's just super good, thank you very much!.. ^_^

The very classic Tetris as a next game would be so nice, and an addition acceleration Fast Forward to the Race game by pressing button (like in a Snake game), will be superb, too!
 
Last edited by mathew77,

mitcha

مجاهد صنديد مقاتل عنيد
Member
Joined
Dec 20, 2015
Messages
405
Trophies
0
Location
collo (chullu)
XP
1,762
Country
Algeria
first let me thank you again @Chrscool8.
let's talk about suggestions , but first let me be nostalgic ;)
i had the original brick game who has only 1 game "Brick Game" or "Tetris"
then comes the 2in1 ,the popular 8in1, 29in1 , 118in1 , most comun now 9999in1

-i remember your 4th game, let's call it "Canon" , who fill the empty squares to make a line.

-another one is the opposit of Canon , let's call it Canon razer , who eliminate all the squares.

-Tank , like the nes version of Tank1990 , you destroy enemy tanks who can shoot a square (missiles) to desdroy you.

Main menu suggestion : the A,B,C.....selection menu is super ,but how if you can choose for example "snake" and see a snake snapshot or snake title on the screen , then Canon ,tetris ....
one per screen when select is pressed (minus).

Per game menu ; idk if this is possible :
-level and speed level choise !
i remember choosing speed level at 12 , who is so fast.

and finally , you implemented speed in Snake by pressing (Zr or Zl) , how about by pressing and Holding the direction button for speed ?
like the original.

i have some ideas of games , can i do it by my self like you suggested in 1st post , keep in mind i have no knowledge in programmation , but have motivation and i'm strong builted muscle man ;)

edit : what @mathew77 suggested is highly recomended.
 
Last edited by mitcha,
  • Like
Reactions: Chrscool8

Chrscool8

Well-Known Member
OP
Member
Joined
Oct 23, 2008
Messages
114
Trophies
1
XP
934
Country
United States
That's just super good, thank you very much!.. ^_^

The very classic Tetris as a next game would be so nice, and an addition acceleration Fast Forward to the Race game by pressing button (like in a Snake game), will be superb, too!

I've been holding off on Tetris since it's the most gamey game, lol. The others I can knock out in an hour or so, but that's gonna take some actual doing! (It's coming, though, I promise. ;) ) Accelerator has been added to Race!

@Chrscool8 Damn, completely forgot about that game. Man you're bringing me so many memories. Thanks! :)
Of course! This is a fun revisit for me, too! Gives me a chance to not only replay these old things but throw in my own tweaks and improvements that I wish they had.

first let me thank you again @Chrscool8.
let's talk about suggestions , but first let me be nostalgic ;)
i had the original brick game who has only 1 game "Brick Game" or "Tetris"
then comes the 2in1 ,the popular 8in1, 29in1 , 118in1 , most comun now 9999in1

-i remember your 4th game, let's call it "Canon" , who fill the empty squares to make a line.

-another one is the opposit of Canon , let's call it Canon razer , who eliminate all the squares.

-Tank , like the nes version of Tank1990 , you destroy enemy tanks who can shoot a square (missiles) to desdroy you.

Main menu suggestion : the A,B,C.....selection menu is super ,but how if you can choose for example "snake" and see a snake snapshot or snake title on the screen , then Canon ,tetris ....
one per screen when select is pressed (minus).

Per game menu ; idk if this is possible :
-level and speed level choise !
i remember choosing speed level at 12 , who is so fast.

and finally , you implemented speed in Snake by pressing (Zr or Zl) , how about by pressing and Holding the direction button for speed ?
like the original.
Thanks for writing in! :D All your suggestions have been noted!
i have some ideas of games , can i do it by my self like you suggested in 1st post , keep in mind i have no knowledge in programmation , but have motivation and i'm strong builted muscle man ;)
Haha, that's the spirit! You may need sooome experience in coding, but perhaps you can figure it out with some example. It's still pretty raw right now, but I'm adding as many helper functions as possible. As of now, you could probably make a "game" in 10 lines of code given my template file. The engine totally handles all the functionality around the gameplay (game switching, controls, drawing, etc, etc) so it's super easy to add new things in. I'm actually thinking of making a short how-to video on ...how to... get started and make a square move around but I should wait a little longer because as I make these minigames, as I come across things that could be helpered, I make the functionality for the next time. In the meantime, I'd be happy to hear ideas of new games or ports that you have! (Draw some pictures!)

Would include an option to hide fps counter, and disable music?
You bet. Next update has you covered. Speaking of which...

----------------

Update!

This update admittedly isn't perfectly polished, but for this whole project I've been going against my normal methodology of 'save updates for 3 months until it's absolutely flawless before releasing' for the sake of just getting it out there. That being said, I don't think it'll crash or anything (fingers crossed).

Here's what's new:

- An interactive menu
-- Automatically handles any amount of games
-- Has page dots at the bottom
-- Retrieves high scores for any game on the list
-- Retrieves animated demos from games
-- Retrieves control list from games
-- Can be returned to from game

- Rowfiller (working-title) has a fail state
- Fast forward enabled for racecar
- New font for titles
- Music toggle button (saved between sessions)
- Sound effect toggle button (saved between sessions)
- Little changes and fixes

Video:



abHnkfj.jpg


d8dG2iD.jpg
 
Last edited by Chrscool8,

mitcha

مجاهد صنديد مقاتل عنيد
Member
Joined
Dec 20, 2015
Messages
405
Trophies
0
Location
collo (chullu)
XP
1,762
Country
Algeria
Haha, that's the spirit! You may need sooome experience in coding, but perhaps you can figure it out with some example. It's still pretty raw right now, but I'm adding as many helper functions as possible. As of now, you could probably make a "game" in 10 lines of code given my template file. The engine totally handles all the functionality around the gameplay (game switching, controls, drawing, etc, etc) so it's super easy to add new things in. I'm actually thinking of making a short how-to video on ...how to... get started and make a square move around but I should wait a little longer because as I make these minigames, as I come across things that could be helpered, I make the functionality for the next time. In the meantime, I'd be happy to hear ideas of new games or ports that you have! (Draw some pictures)
so you want my secret games to became rich , okay , i'm ok with that lol
here is some titles : staires , anti pong , hole , pong 2 players ( 1p control horizontal pads, the other vertical one's ) and there are more in my little mind.
i will draw some this week end, to have a better idea.

as for learning coding from scratch , i have the motivation and i will at least learn some techniques , as for your tutorial ( no pression ) video or text , it's a major event if it's comes true ,cuz i will end up writing a game even in 5 years.

my only enemy for learning is Time.

thank you in advance for your efforts
 

Chrscool8

Well-Known Member
OP
Member
Joined
Oct 23, 2008
Messages
114
Trophies
1
XP
934
Country
United States
so you want my secret games to became rich , okay , i'm ok with that lol
here is some titles : staires , anti pong , hole , pong 2 players ( 1p control horizontal pads, the other vertical one's ) and there are more in my little mind.
i will draw some this week end, to have a better idea.

as for learning coding from scratch , i have the motivation and i will at least learn some techniques , as for your tutorial ( no pression ) video or text , it's a major event if it's comes true ,cuz i will end up writing a game even in 5 years.

my only enemy for learning is Time.

thank you in advance for your efforts
Sounds good to me! I'll see what I can do!

----------------

Yesterday, I was at my parents' place with the mission in my head to find my old game in the basement (aka the place of never seeing things again) and impossibly, it appeared in the very first box I checked!! This was my original machine from almost 20 years, marked 2002! It's a "118 in 1" and plays that classic fast paced Ode to Joy rendition. Works perfect (with a new battery). I spent a lot of car rides playing this thing!

9LRx8Jw.jpg

--------

Also I added a "guess if the number is higher or lower or the same" game to the app!
 

Chrscool8

Well-Known Member
OP
Member
Joined
Oct 23, 2008
Messages
114
Trophies
1
XP
934
Country
United States
Hey there! Long time, no see, lol. Sorry about the slow updates. Been busy job hunting and interviewing for the last month or so since unfortunately these projects don't pay the bills. The last few days, I have been working on it, though! I've been untangling the mess of code to make it make more sense and also allow for future cross-platform abilities (especially being able to compile for PC).

PS:
IoTcZGZ.jpeg
 
Last edited by Chrscool8,

lordelan

Well-Known Member
Member
Joined
Jan 4, 2015
Messages
5,788
Trophies
1
Age
44
XP
6,520
Country
Germany
Hey there! Long time, no see, lol. Sorry about the slow updates. Been busy job hunting and interviewing for the last month or so since unfortunately these projects don't pay the bills. The last few days, I have been working on it, though! I've been untangling the mess of code to make it make more sense and also allow for future cross-platform abilities (especially being able to compile for PC).

PS:
IoTcZGZ.jpeg
Sounds great! I'd especiall appreciate a Libretro core if ever possible no matter if u ship the games as roms or include them directly as it is now. That would be really cool but take your time and finish the project first to a state where u r very happy with it.
 

Chrscool8

Well-Known Member
OP
Member
Joined
Oct 23, 2008
Messages
114
Trophies
1
XP
934
Country
United States
Sounds great! I'd especiall appreciate a Libretro core if ever possible no matter if u ship the games as roms or include them directly as it is now. That would be really cool but take your time and finish the project first to a state where u r very happy with it.

Well that’s a fascinating idea that I hadn’t considered! I bet that would save me tons of frustration over manually compiling for every api.
 
  • Like
Reactions: lordelan

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    K3Nv2 @ K3Nv2: Well start walking towards them +1