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, 1 hours, 46 minutes and 10 seconds old. Please keep that in mind.

43 - Border Collision If you're doing a game or something like this, you'll want to make sure the pixels that wouldn't make it on screen don't get placed in off screen areas, because we can't know the effects that it would cause. So, this time, I'm doing a per-pixel check. This is mearly a tester.

main.cpp:
#include <stdio.h> //------------------------------------------------------------------------- #include "STARS.H" #include "DRAWABLE.H" //------------------------------------------------------------------------- star starfield[starnum]; int scroll = 0; drawable tester = drawable(100, 100); int flags; //------------------------------------------------------------------------- int main(){ return easymode(); } //------------------------------------------------------------------------- extern "C" int init(){ srand(time(0)); for(int i = 0; i < starnum; i++) starfield[i] = star(); return 0; } //------------------------------------------------------------------------- extern "C" int cleanup(){ return 0; } //------------------------------------------------------------------------- extern "C" int draw(){ for (int i = 0; i < starnum; i++) starfield[i].draw(scroll, 0); scroll++; if(keymap[KEY_up]) tester.y--; //Remember, Y is a little backwards from what we remember in school if(keymap[KEY_down]) tester.y++; if(keymap[KEY_left]) tester.x--; if(keymap[KEY_right]) tester.x++; tester.draw(); return 1; }

DRAWABLE.H:
#ifndef _DRAWABLE_H #include "SETTINGS.H" class drawable{ protected: unsigned char* bitmap; //for emplace void emplace(); //Ensures only partial bits of the image actually draws if off screen. bool offscreen; public: int x, y; unsigned short xs, ys; drawable(); //Hopefully no one uses this one intentionally, but it'll be used by necessity via arrays. drawable(int x, int y); void draw(); }; #endif #define _DRAWABLE_H true

DRAWABLE.CPP:
#include "DRAWABLE.H" //------------------------------------------------------------------------- drawable::drawable(){ //No one should really use this one. x = -100000; y = -100000; } //------------------------------------------------------------------------- void drawable::emplace(){ //This could really, really benefit from rewriting into assembly. offscreen = true; int rx = x - (xs >> 1), ry = y - (ys >> 1); for(int i = 0; i < ys; i++) for(int j = 0; j < xs; j++){ int px = rx + j; //We're declaring here hoping the compiler doesn't actually create the variables int py = (ry + i)*xsize; if(py < 0 || py >= ysize * xsize) break; //Push up Y, since the whole row's dead. if(px &t;= 0 && px <= xsize){ offscreen = false; //Lots of memory ops in a loop. buffer[py+px] = bitmap[(xs*i)+j]; } } } //------------------------------------------------------------------------- drawable::drawable(int x, int y){ this->x = x; this->y = y; this->xs = 10; this->ys = 3; } unsigned char dummy[] = { " x " //Notice the lack of commas? The 0 is being taken away automatically and all is thrown into 1 long string. "x x" " x " }; void drawable::draw(){ bitmap = dummy; emplace(); } //-------------------------------------------------------------------------

Ok, as for DRAWABLE.CPP, dummy[], draw(), and the last two lines of drawable(int, int) are going to be erased by the next lesson, so once you're done playing around, delete them. They're just fillers to help us test everything out right now. The offscreen bool is to help us deal with bullets and enemies that aren't on screen, anymore: if you dynamically allocate (using malloc or something like that) on your own, you'll want to do that for enemy ships that pass the player. Right now, we'll just use it to recycle ammo, which we haven't created yet.

#ifndef _DRAWABLE_H #include "SETTINGS.H" class drawable{ protected: unsigned char* bitmap; //for emplace void emplace(); //Ensures only partial bits of the image actually draws if off screen. bool offscreen; public: int x, y; unsigned short xs, ys; drawable(); //Hopefully no one uses this one intentionally, but it'll be used by necessity via arrays. drawable(int x, int y); virtual void draw() = 0; }; #endif #define _DRAWABLE_H true

This file should looke like this after you're done messing around. In the next lesson, you'll see why. For now, just know it means that "children" must do some chores.

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