This page was last updated on March 13th, 2020(UTC) and it is currently May 30th, 2023(UTC).
That means this page is 3 years, 78 days, 3 hours, 21 minutes and 54 seconds old. Please keep that in mind.

41 - Setting Up for the Future Ok, now we're going to dig deeper by taking advantage of our H (header) files.

//Defines are compiletime only variables. #define xsize 320 #define ysize 200 #define starnum 200 //------------------------------------------------------------------------- extern "C" void srand(int seed); extern "C" int rand(); extern "C" int time(int ptr); //------------------------------------------------------------------------- extern "C" int easymode(); extern "C" char buffer[0xfa00];

I saved that as "SETTINGS.H", and this time the filename will be sort of important, because we're going to reference these things over many files.

#include "SETTINGS.H" class star { int x, y; public: star(); void draw(int px, int py); };

Notice the functions don't have brackets. We can do this outside of classes, too. They're called "templates." The idea is that you can have a file with things that need to be referenced, but don't need to be defined right away ('cause maybe you want to save on compile time by taking advantage of .o files while debugging). That's essentially what we've been doing with the extern stuff, except extern can be omitted when not using "C" with it to force it to be C-style. If we are going to reference classes and variables, C++ likes to nanny us, so we need at least a prototype of a class, like above, so that C++ has enough information per file to do that ("#incldue" makes the code part of the same source file at compile time, as opposed to simply linking things together). This file was saved as "STARS.H".

#include "STARS.H" //------------------------------------------------------------------------- star::star(){ x = rand() % xsize; y = rand() % ysize; } //------------------------------------------------------------------------- void star::draw(int px, int py){ if((y - py) < 0){ y = ysize + py; x = rand() % xsize - px; } else if((y - py) > ysize){ y = py; x = rand() % xsize - px; } if((x - px) < 0) { x = xsize + px; y = rand() % ysize - py; } else if ((x - px) > xsize){ x = px; y = rand() % ysize - py; } buffer[(y - py) * xsize + (x - px)] = 0x0f; }

Functions that have been prototyped don't need anything special added to them, except in this case we have to because we're not putting the function definitions in the class defintion's brackets. How does it know which class "draw" is assigned to if we need a "draw" that applies to another class? Notice this also includes "STARS.H", which means we're making the compiler read it twice. On the flip side, modifying this file to modify the star code no longer requires we recompile main.cpp since we're not changing it anymore, and vice versa. This means it takes longer for someone else to compile if since they won't have the .o files, but it does mean it's less time for us to compile when making little changes. Congrats on "refactoring" for the first time, too. Hopefully you don't have to do it alot outside of this setting (plan ahead to reduce this). Keep in mind that, depending on how makefile changes, it may or may not detect changes in a header file as invalidating an object file.

Get your own web kitty here!
©Copyright 2010-2023. All rights reserved.