|
OBEYBREW.COM | Tutorials | A Crash Course In HuC Part 3 A Crash Course In HuCPart 3 - Gone LoopyNow, we're going to get into some pretty fun stuff. Not only will we be covering two more extremely important parts of coding the PCE here, but we'll also be making some in-progress source changes. This is to make sure you're really paying attention!What You'll LearnThis lesson will teach you how to use for loops and will introduce sprites. We'll also have a little more fun with color.It's Not A Soft DrinkNo, a sprite isn't found in a bottle, it's found on the screen. So, what's a sprite? In simplest terms, it's a collection of pixels that is independent of the background... that is to say, a sprite can move on its own without disturbing the pixels of the background.Setting Up Our ProgramYou already know how to start your program by now. But this time, we're going to add a couple of new things: a palette and a sprite. Before your main(), add the following lines of code:const int paldata[] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0 }; const int sprdata[] = { 1001, 2002, 3003, 4004, 5005, 6006, 7007, 8008, 9009, 10010, 11011, 12012, 13013, 14014, 15015, 16016 }; int myarray[5]; So now, we have a sprite block and a palette. Now what? Pour It Onto The ScreenNow, we need to see if we can put that sprite onto the screen. Setting up a sprite can be a lot of work, as there are a lot of details to see to. The first thing we have to do is initialize the sprite handler. This is done with a single line of code.init_satb(); The next thing we have to do is set all the details of the sprite. Any time we're working with sprites, we have to first determine which one we're working with. There are 64 sprites available to us. For now, let let's just use the first one. To select it, we use: spr_set(0); spr_x(0); spr_y(0); So now we know where our sprite is going to be. But there there's a few other things we have to do as well. spr_pal(0); spr_pri(1); spr_pattern(0x5000); spr_ctrl(FLIP_MAS|SIZE_MAS,NO_FLIP|SZ_16x16); Where's Mah Pixels At?If you try compiling and running this code right now, you won't see anything on the screen just yet. Even though this is technically a complete sprite, there's still a few things left to do. For starters, there there's nothing currently at 0x5000 in VRAM so let's put something there.load_vram(0x5000, sprdata, 0x40); You're still not going to see anything though. Why not? Well, we don't have any colors defined yet either. load_palette(16, paldata, 1); Guess what though, you're still not going to see a sprite! Yes, it's defined and it has color, but the video hardware doesn't yet have the updated information. So, we give it one last detail: satb_update(); Getting Into The LoopHaving a sprite just sitting there staring at us isn't very interesting. Let's make it move. Now, you need to create a variable. If you're used to normal C, you'd think you can just create a variable right here in the code and be done with it. However, in HuC, all variables have to be defined at the top of the function. Yeah, it's odd, but it it's how it works so no whining!Put this at the top of your main function: int posx; for(posx=0; posx<256; posx++) { Ok, so now we have the start of a loop. Now, let let's put something inside of it. Let's mess with the sprite's X position. spr_set(0); spr_x(posx); satb_update(); vsync(3); Notice that we have a curly brace right after the for line? Our loop is finished, so we have to close it. We close it the same way we'd close a function: } How Flashy!Ok so the moving sprite is kind of cool. But let's do something even more interesting on top of it... let's mess with its colors. In the process, we're going to do another very important part of coding... nested loops. No, Tweety is not helping you.So we're going to need another variable. In fact, we're going to need two new variables. Create two new int variables name them colorchange and result. Remember how to do this? Nowm let's make a new for loop inside the existing for loop. Set it up right after the satb_update call. colorchange is the variable we'll use, and we'll start it at 257 and make it increase by 1 until it reaches 272... got all that? for(colorchange=257; colorchange<272; colorchange++) { result = random(255) + random(255); So then, we have a random color maker. Now, let's actually mess with the palette with this data! set_color(colorchange, result); } Moving right along... that's all there is to this loop, so close the loop and compile your program. Hooray! Flashing moving thing! Movin' Down The ScreenSo that's pretty interesting and all, but the sprite is still stuck at the top of the screen. Let's have at some more random fun. Add this single line of code right before your satb_update:spr_y(spr_get_y() + random(3)); Yep. If you want to mess with the values of sprdata to see different pixels... knock yerself out. So...What's Next?At this point, you have enough knowledge to handle the vast majority of programming in HuC. If you've mastered it up to this point, congratulations! Colors, sprites, variables, loops, and a whole mess of other fun stuff is out of the way. From here, we'll expand on our existing knowledge by adding interaction and we'll also dive into some background stuff too. It gets pretty easy from here.Full Program Listing#include "huc.h" const int paldata[] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0 }; const int sprdata[] = { 1001, 2002, 3003, 4004, 5005, 6006, 7007, 8008, 9009, 10010, 11011, 12012, 13013, 14014, 15015, 16016 }; main() { int posx, colorchange, result; init_satb(); spr_set(0); spr_x(0); spr_y(0); spr_pal(0); spr_pri(1); spr_pattern(0x5000); spr_ctrl(FLIP_MAS|SIZE_MAS,NO_FLIP|SZ_16x16); load_vram(0x5000, sprdata, 0x40); load_palette(16,paldata,1); satb_update(); for (posx = 0; posx < 256; posx++) { spr_set(0); spr_x(posx); spr_y(spr_get_y() + random(3)); satb_update(); for(colorchange = 257; colorchange < 272; colorchange++) { result = random(255) + random(255); set_color(colorchange,result); } vsync(3); } } See AlsoA Crash Course In HuC - Part 4 | init_satb | spr_set | spr_x | spr_y | spr_pal | spr_pri | spr_pattern | spr_ctrl | load_vram | load_palette | satb_update | random | set_color | vsync | Useful Source Code |