Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 4 5 [6] 7 8 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 817486 times)

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #75 on: January 06, 2012, 12:53:55 am »

I was going to go into c#, but I can do Java.
Think we are good to start with the GRASP patterns, or take a step back and explain the syntax?

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #76 on: January 06, 2012, 01:14:12 am »

Given what he's said, a syntax lesson is definitely in order.  He mentions not just completing 'hello world' but struggling with it.  This'll either be one of the guys that can't get on or one of the guys that only changes on through hand on just doing it.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #77 on: January 06, 2012, 01:40:38 am »

Eh, you can get started on the basics of the basics then. I'll start with objects, for people like Aquizar that have that down.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #78 on: January 06, 2012, 01:45:05 am »

Lesson 1.0: Classes on classes
Fuck where do I even start with this? Well I guess trying to explain what an object is would be a good place. You need to understand what is a class, and what is an object, and the difference between them.

Think of a 'class' as a category of things, while an 'object' is an instance of this sort of thing. The class provides the basic blueprints that are used to build objects, while the objects themselves are acted upon.

Imagine if I designed a building and had blueprints for it. The blueprints are like the class, but until it is built I can't go inside it or see it or touch it. No instance of my building yet exists. It is just a theoretical thing.

Now what if I hire builders to go off and build five different buildings in different blocks of land with these blueprints. These are instances of my class. They might look the same, and have the same function, but they are not the same building. If I paint the walls of one of them blue, then don't all suddenly turn blue. In this way, although each object follows the same blueprints, they are not the same object, and they can all have unique attributes.

Now for the fun bit, we get to program our own class. I figure it will be easy if we start with a 'Person' class, and go from there.

First we need to think about what attributes a person has. What is different and unique about people that we can store in a computer? Well we can handle strings of characters, so the persons name is a good start. We can also handle ints very easily, so let's use that for age.

Secondly, we need to think about what a person can do. There is a metric shitton of things people can do. Don't even get me started! As such, let's keep things simple and make out people talk.

So that is a person, as they exist in out program. They have a name and age, and the ability to talk. Cool! So how do we program this?

Firstly, if you are following along in c# I want you to open visual studio and make a new console application. Any other language and I hope you can still follow along, otherwise just ask me and I will provide a demo in your language of choice. Call this new project 'Lesson 1' and save it in a new folder where ever you want called 'Max White is awesome and I will do everything he says'

Hopefully visual studio generated some code for you, don't freak out. If you do freak out, go back to the basics for a few lessons, then come back. Just ignore this for now. Now, I want you to press Shift + Alt + C. This is your shortcut for adding a new item. Select 'class' and call it 'Person.cs', and make sure to pay attention to upper and lower case!

BEHOLD YOUR CLASS! No shit, you just made a class. It is just that simple. The problem is that right now it doesn’t do anything. Let's change that.
Please note: Dear good programmers, yes I know I am showing them a bad habit or two, but the lesson on constructors comes next. Give me a break.

Now, add 'public string Name;' and 'public int Age;' under the second { so that they are both inside the bracers. There, your people now have a name and an age, how easy is that, right! Well now we are going to add a method. I shall explain a bit more about it soon, but ours looks like this.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lesson_1
{
    class Person
    {
        public string Name;
        public int Age;

        public void Talk()
        {
            Console.WriteLine("Hello, my name is {0} and I am {1} years old!", Name, Age);
        }
    }
}

There, a person with a name, age and the ability to talk. How awesome are we! Not that awesome. Remember when I said that you can't go inside a building until you build it? Well you can't make a person talk until you make a person. We have the blue prints, but no people! Let's fix that with a person. On the solution explore on the right side, double click program, and add the following. I'll go into more depth soon, relax.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person guy = new Person();
            guy.Name = "Steve Jobs";
            guy.Age = 9001;
            guy.Talk();

            Console.ReadKey();
        }
    }
}

Behold! The dead doth rise! Have fun fucking with that while I write up a better explanation of some of the stuff you just used. Remember, this is the most basic block of some really cool shit. One day you will be about to wear a shirt that says 'I know polymorphism, bitch!'

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #79 on: January 06, 2012, 01:56:27 am »

Lesson 1.1: Public?
You might have noticed that I told you to add 'public' before anything in your class. Try removing that and running the program again. Any luck? No, I thought not.

Classes have attributes. In our Person class, they have two attributes, Name and Age. However, there are different types of attributes. Four in fact, these are public, private, protected and internal. Right now I want you to forget the last two, you won't need to think about these yet, we are going to focus on the first two.

When an attribute is public, anything can access it. It is open to be read and written by any other class. When an  attribute is private, only the class it is within can do anything to it. By default in c# attribute variables are private, meaning that having nothing in front of a variables is the same as having 'private' there. Java works in the opposite manner, in that things are public by default. This is because Java is inferior, and I plan to mock it during any lessons to do with delegates, or indexes, partial classes, or foreach loops.

Lesson 1.2: public void Talk()
Now, hopefully you remember what public means, right? Well it works for methods as well as attributes. If you make a method 'private' then only the class itself will be able to call it, but as we want other things to make the person talk, it needs to be public.
Void means that there is no return type. When this method is called, it doesn’t give anything back. Later on we will be using things with return types, so it should become more clear.
'Talk' is the name of the method. That is what you use to invoke it.
() is where we put parameters. These are things we tell the method for it to work. In this case, it will go fine without anything, so we have empty brackets.

I'm hoping you have already messed around with Console.WriteLine before, but this has a cool trick in it. Within the string you want to write, you can include {number}, and have that relate to anything else you add to the methods parameters. This is useful as duct tape if your using the Console.

Lesson 1.3: Onto the main.
We start with declaring a Person with 'Person guy'
'guy' is the name of the variable, and how we refer to it, how we use it. We could have used anything here, and replaced 'guy' with that new thing anywhere. In fact, lets do that now!

Right click Guy > Refractor > Rename...
In the New Name space, change it to 'dude', click Ok and Accept. Isn't Visual Studio awesome? It just changed 'guy' to 'dude' throughout your code. That is a really nifty trick sometimes if you suck at spelling like me.
Run the code again. Any change? Nope, none. Like I said, it doesn’t matter what we call the variable, it will run fine.

We then have = new Person()
That is the same as going to the construction workers and asking them to make a new building. The 'new' keyword summons your army of workers, and the Person() hands them the blueprints. It is really a constructor, and later on you will be able to give them more details like givin the person a name on construction, but this works for now.

Now, dude.Name = “Steve Jobs” is telling dude what he's name is. You are assigning he's name. Like I said before, you can only do this because it is public. Otherwise you could only assign it within the Person Class

Lesson 1.4: UML
I'm sure you know what a flow chart is. You know that thing that shows repetition and selection and iteration? It is pretty sueful, but it falls flat when classes get involved. As such, we have something new for showing class structure, and it is called a class diagram.
There are different ways to draw a class diagram, but a some what universal way is UML. UML stands for United Modeling Language, and it applies to a lot more than class diagrams. There are use cases, and system sequence diagrams, and all sorts of cool shit, but I figure keep it to the class diagram for now, as it will suit what we need.

It is important to realize that we have a very, very simple class structure here, so you can expect a very simple class diagram. Because we only have one class in our program, you can expect only one class in our diagram, and here it is.



This diagram was made in Microsoft Visio, but there are other programs that can make these, some with free versions. Visual Paradigm and Rational Rose are two off the top of my head. Otherwise, it is fine to just draw these by hand, just like any other diagram.

First thing to notice is that we have a box. You see that box? That is a class. Every class gets a box, it's a rule. Each box is separated into three parts.
The first part is just the class name. It is pretty simple right now, but the way that you write the class name means stuff. Seriously, you need to write abstract classes in a different way, but don't worry about that for now.
The next part shows the attributes. It follows the convention -{name} : {type} and that is pretty much all you need to worry about it for now.
The last part of the box is for methods. It has a very similar convention to attributes, but a little more detailed with +{name}({parameters}) : {return type}

It might not seem like much now, but in larger programs, being able to see the flow of data around your program makes life a lot easier. This is the road map to your program, if only there was more than one place to go...

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #80 on: January 06, 2012, 07:01:47 am »

Is there any other forum out there (that isn't specialized on programming help) where people would willingly spend lots of their time to write and post great tutorials on request of a single person?
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #81 on: January 06, 2012, 08:17:24 am »

I may start building off of Max White's tutorials, providing C++ and Java examples for doing the same things.

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #82 on: January 06, 2012, 08:42:25 am »

I'm making a little program on the GBA intended to simulate the box you get when you click and drag around stuff on the PC. Currently it draws a solid box, which at least is closer than it used to be, but if you draw left or up, the box doesn't appear. I'm not sure how many of your are familiar with the GBA stuff we're using, but I'm hoping it's a problem in the rest of the coding.
Code: [Select]
#include <stdint.h>
#include "gba.h"
#include <stdlib.h>

void DrawBox(int x, int y, int x_length, int y_length, int colour);
void DrawReticule(int x, int y);
void ClearScreen8(uint8_t);
void FlipBuffers();
void GetMovement(int &x, int &y);

int main()
{
int reticule_x = 20;
int reticule_y = 20;
int box_x = 0;
int box_y = 0;
bool do_once = 0;
int prev_buttons;

REG_DISPCNT = MODE4 | BG2_ENABLE;

SetPaletteBG(3,RGB(0,0,0));
SetPaletteBG(1,RGB(31,31,31));
SetPaletteBG(2,RGB(31,0,0));

while(1)
{
FlipBuffers();
ClearScreen8(3);
GetMovement(reticule_x,reticule_y);
DrawReticule(reticule_x,reticule_y);
while ((REG_P1 & KEY_A)==0){ // REG_P1 is the register that tells if a key is pressed or not. Each bit corresponds to a different key, so all this is doing is seeing if A is being pressed.
if (do_once == 0){
box_x=reticule_x;
box_y=reticule_y;
do_once = 1;
}
FlipBuffers();
ClearScreen8(3);
GetMovement(reticule_x,reticule_y);
DrawReticule(reticule_x,reticule_y);
DrawBox(box_x,box_y,(reticule_x-box_x+1),(reticule_y-box_y+1),1);
}
do_once = 0;


prev_buttons=REG_P1;
WaitVSync();
}
return 0;
}


void DrawBox(int x, int y, int x_length, int y_length, int colour)
{
for (int i = 0;i<y_length;i++){
for (int j = 0;j<x_length;j++)
PlotPixel8((x+j),(y+i),colour);
}
}

void DrawReticule(int x, int y)
{
DrawBox((x-5),y,11,1,1);
DrawBox(x,(y-5),1,11,1);
}

void GetMovement(int &x, int &y)
{
if ((REG_P1 & KEY_UP)==0)
y-=1;
if ((REG_P1 & KEY_DOWN)==0)
y+=1;
if ((REG_P1 & KEY_RIGHT)==0)
x+=1;
if ((REG_P1 & KEY_LEFT)==0)
x-=1;
}
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #83 on: January 06, 2012, 10:49:36 am »

The GBA has a C compiler? That's cool.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #84 on: January 06, 2012, 11:00:39 am »

Okay, ignoring the tutorial conversation for a moment:

Are "classes" in languages like Ruby really, technically, classes at all? They are certainly a bit strange, especially since objects can have methods that different completely from their classes, classes are objects in their own right... Heck, you can have a class that exists only to create classes.

Honestly, they feel a lot more like factories than real classes to me. But maybe I'm just in a weird mood in what I'm currently thinking of as "real classes".

Also, I'm reading the classic OO Patterns book (Design Patterns: Elements of Reusable Object-Oriented Software) Has anyone else read this book? I'm actually surprised at how many of these patterns I'm familiar with and use, though it's nice to get a more formal language for understanding and discussing them.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #85 on: January 06, 2012, 12:27:07 pm »

I feel behind for taking the time to go to bed...and I should probably do my homework at some point...

Stargrasper's Java Basics Tutorial Series

Also, a forewarning, really early Java lessons include a lot of hand waving and magic.  Stuff you just aren't expected to understand (yet) and you just have to accept it works like I say it does.

Lesson 0.0: The Very Beginning
For starters, lets look at 'Hello World'.
Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson1;

public class Main {

public static void main(String[] args) {
System.out.println("Hello World");
}

}

This should look familiar to anyone who has done any programming whatsoever.  It's the introductory program used by everyone for the last thirty years.  Lets dissect it.

Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson1;Java requires all code to be contained in a package.  Basically, it's a folder tree that organizes the code in a meaningful way.  In this case, you'll find my code file, 'Main.java', in the folder tree my_workspace/com/bay12forums/stargrasper/javatutorial/lesson1/Main.java.  Furthermore, Java requires every single individual code file to have a package line telling the compiler where to find that file relative to the root directory of the project.  The original Java specifications from Sun Microsystems, now a part of Oracle, say that packages should be named as 'reverse URLs'.  Think of how internet subdomain URLs are formed and then reverse the order elements appear.  It's demonstrating the where to find it from the largest network to the smallest.  Reading the package, we know it's a com, there's a lot of comes.  We also know it's part of bay12forums.  That narrows things, but not nearly enough.  Looking farther, we know it has specifically to do with the user stargrasper.  Okay, now we're talking.  But lets make it easier, farther still, we can see it's part of the series javatutorial and even farther, that it's part of lesson1.  Sun's expecting you to give a lot of information on this one line.

If that's a bit much for you, just know that you need the line to tell Java where to find your stuff.

Code: [Select]
public class Main {And now to the stuff you probably care about.  public and class are keywords that I'll get to later, though I'll refer you to Max's tutorial for class work.  Main is just the name of the class.  This is where I start waving my hands like a Jedi and saying it's magic.  You aren't expected to understand anything on this line.

Code: [Select]
public static void main(String[] args) {*Hand-wavey magic*
This starts the main() method.  All Java programs run as local applications, that is, not internet applets, need a main() method.  It's what tells Java where to start executing your code.  Everything else about this line is magic that we'll worry about later.

Code: [Select]
System.out.println("Hello World");And the one line where we do stuff...  Every word, period, quotation, and semicolon means something.  For now, System.out.println() is how you output text strings to the console.  You put it inside the parenthesis with quotations marks around it unless it's a String variable (magic we'll talk about in a couple lessons) and you end the line with a semicolon.  In Java, lines of code are terminated by a semicolon, not the end of the line you're writing on.

You'll notice the last couple of lines are just closing brackets.  We'll get to those later.  For now, they're magic.

Lesson 0.1: Whitespace, Comments, and Code Style
Throughout my lessons, I'm going to be pushing a lot of stylistic practices that mean nothing to Java but make the code easier to read, both for yourself and the rest of us when you show it asking for help.  It doesn't help if you made the code work if you can't figure out how it works in six months when you're debugging something.

My rule: It should never take longer than five seconds to understand a block of code after reading it.

If that won't be true six months from when you last saw the code, you need to do something about it.

The first thing I do in include a lot of whitespace.  That is, space that nothing is written in.  It's not necessary by any means, Java ignores it entirely.  Here's Hello World in two perfectly legal styles that I'll shoot you for if you use:

Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson1;public class MainOneLine {public static void main(String[] args) {System.out.println("Hello World On One Line");}}This is really hard to read.  It's totally and completely legal code, but really hard to read.  If you show me this, I will refuse to help you.  It's not that hard for someone at my skill level to read that, but I don't want to spend more than a couple of seconds trying.  More importantly, in a few months when you reference this code again, you'll never be able to figure it out.  Always separate code on several lines.

Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson1;
public class MainNoWhitespace {
public static void main(String[] args) {
System.out.println("Hello World With No Whitespace");
}
}
Okay, this doesn't look so bad.  That's because it's such a simple example to start with.  Later on I'll show you examples that are much harder to read without extra whitespace.  For now, I'm teaching you.  Follow my coding practices.

Splitting the code up with whitespace makes it easier to read and has no effect on the code itself.  It's a stylistic practice that I'd really like everyone to use because code without whitespace it annoying to read.

Now...comments.  Here is both a description of comments as well as an example of them in our Hello World program.
Code: [Select]
/*
 * Written by Stargrasper
 * Updated 1/6/2012
 * (c) Stargrasper 2012
 *
 * Comments are text that is written within the code
 * that is completely ignored by the compiler.  That
 * means that you can leave notes in the code.  Either
 * use it to describe what a complex block of code
 * is doing or use it to leave notes for yourself or
 * for someone else that has to work with the code.
 *
 * You should always, always, always be commenting your
 * code.  It's important.  You'll see me not do it a lot.
 * Do as I say, not as I do. :P
 *
 * There is usually a big comment at the beginning of a
 * code file that says who wrote it, when it was updated,
 * who owns it, and any other relevant information about
 * the file and content itself.
 *
 * Java used C++ style comments.  That means there are
 * two types of comments.  Multiline comments start with
 * a '/*' and end with the same in reverse.  Everything
 * in between is considered a comment and is thus
 * ignored by Java.  This is a multiline comment.
 *
 * The other type is the single line comment.  It starts
 * with '//' and everything on that line of text
 * thereafter is considered a comment and ignored by
 * Java.
 */

/*
 *  Package description.  States where to find code file.
 *  Worry about it later.
 */
package com.bay12forums.stargrasper.javatutorial.lesson1;

// Class description.  Worry about it later.
public class MainWithComments {

// main() method.  Again, we'll work about it later.
public static void main(String[] args) {
// Outputs text string to the console.
System.out.println("Hello World With Comments");
}

}

My personal rule on comments is that I'll only comment something if it will take me longer than five seconds to figure out after not seeing it for six months.  Consequently, I don't comment things I consider obvious or easy to figure out.  That being said, I think all of you should comment everything.  I will make an effort from this point forward to comment every example I use in tutorials.

Coding style is just that, style.  Unlike whitespace and comments, specific elements of style, there are some elements that can and will dramatically effect the code.  I'll teach practices that minimize future headaches, though I'll try to remember to teach alternate methods of doing things even if I don't consider them the best stylistic option.  Sometimes I'll even use these options myself for one reason or other.

Post Epilogue

Feel bad yet?  We haven't gotten past Hello World and I just wrote a solid page or so of tutorial on minimal basics.  We didn't do much of any actual coding, but demonstrated some background of how Java thinks and stylistic practices that are darn near necessary.  Comments in particular and necessary and usually aren't described in tutorials.

I promise this afternoon when I write the next tutorial, there will be lessons on how to actually write code.

Okay, I'll be back in an hour or two to write the next Basics tutorials.  I need to, you know...eat breakfast, shower, and consider when I'm going to do my homework that's due tomorrow.  Back soon!

QUICK EDIT: Do you guys want me to give a tutorial on Eclipse, Netbeans, etc?
« Last Edit: January 06, 2012, 12:31:32 pm by Stargrasper »
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #86 on: January 06, 2012, 12:31:22 pm »

Tutorials that explain what everything does are always good.

Unlike those that tell you "here's some code, that's what it does, have fun figuring out how to use it"
Logged

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #87 on: January 06, 2012, 12:38:19 pm »

The GBA has a C compiler? That's cool.
Okay, I was unclear. I'm writing in C/C++ (it's supposed to be C but doesn't mind C++) and it's being compiled to run on the GBA.
Logged

Shakerag

  • Bay Watcher
  • Just here for the schadenfreude.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #88 on: January 06, 2012, 01:02:11 pm »

Posting to watch, and to wonder how many mainframe/OS390/zOS programmers are about.  Because I'm pretty fuzzy on everything else (C++/C#/Java/FORTRAN/COBOL) other than that right now. 

Oh, and JCL, I suppose.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #89 on: January 06, 2012, 02:04:45 pm »

I'm making a little program on the GBA intended to simulate the box you get when you click and drag around stuff on the PC. Currently it draws a solid box, which at least is closer than it used to be, but if you draw left or up, the box doesn't appear. I'm not sure how many of your are familiar with the GBA stuff we're using, but I'm hoping it's a problem in the rest of the coding.
Code: [Select]
#include <stdint.h>
#include "gba.h"
#include <stdlib.h>

void DrawBox(int x, int y, int x_length, int y_length, int colour);
void DrawReticule(int x, int y);
void ClearScreen8(uint8_t);
void FlipBuffers();
void GetMovement(int &x, int &y);

int main()
{
int reticule_x = 20;
int reticule_y = 20;
int box_x = 0;
int box_y = 0;
bool do_once = 0;
int prev_buttons;

REG_DISPCNT = MODE4 | BG2_ENABLE;

SetPaletteBG(3,RGB(0,0,0));
SetPaletteBG(1,RGB(31,31,31));
SetPaletteBG(2,RGB(31,0,0));

while(1)
{
FlipBuffers();
ClearScreen8(3);
GetMovement(reticule_x,reticule_y);
DrawReticule(reticule_x,reticule_y);
while ((REG_P1 & KEY_A)==0){ // REG_P1 is the register that tells if a key is pressed or not. Each bit corresponds to a different key, so all this is doing is seeing if A is being pressed.
if (do_once == 0){
box_x=reticule_x;
box_y=reticule_y;
do_once = 1;
}
FlipBuffers();
ClearScreen8(3);
GetMovement(reticule_x,reticule_y);
DrawReticule(reticule_x,reticule_y);
DrawBox(box_x,box_y,(reticule_x-box_x+1),(reticule_y-box_y+1),1);
}
do_once = 0;


prev_buttons=REG_P1;
WaitVSync();
}
return 0;
}


void DrawBox(int x, int y, int x_length, int y_length, int colour)
{
for (int i = 0;i<y_length;i++){
for (int j = 0;j<x_length;j++)
PlotPixel8((x+j),(y+i),colour);
}
}

void DrawReticule(int x, int y)
{
DrawBox((x-5),y,11,1,1);
DrawBox(x,(y-5),1,11,1);
}

void GetMovement(int &x, int &y)
{
if ((REG_P1 & KEY_UP)==0)
y-=1;
if ((REG_P1 & KEY_DOWN)==0)
y+=1;
if ((REG_P1 & KEY_RIGHT)==0)
x+=1;
if ((REG_P1 & KEY_LEFT)==0)
x-=1;
}


You need to change this:
Code: [Select]
DrawBox(box_x,box_y,(reticule_x-box_x+1),(reticule_y-box_y+1),1);

into this:
Code: [Select]
int left, top, width, height;
left = _min(box_x, reticule_x);
width = box_x + reticule_x - 2*left;
top = _min(box_y, reticule_y);
height = box_y + reticule_y - 2*top;
DrawBox(left, top, width+1, height+1, 1);

And add a _min() function somewhere:
Code: [Select]
int _min (int x, int y) {
return((x < y) ? x : y);
}

If you need a hollow box:

Code: [Select]
void DrawHollowBox(int x, int y, int x_length, int y_length, int colour) {
int i;
for (i = y; i <= y + y_length; i++) {
PlotPixel8(x, i, colour);
PlotPixel8(x+x_length, i, colour);
}
for (i = x; i <= x + x_length; i++) {
PlotPixel8(i, y, colour);
PlotPixel8(i, y+y_length, colour);
}
}

To call the new function:
Replace this
Code: [Select]
DrawBox(left, top, width+1, height+1, 1);
with this.
Code: [Select]
DrawHollowBox(left, top, width, height, 1);
« Last Edit: January 06, 2012, 04:16:30 pm by MagmaMcFry »
Logged
Pages: 1 ... 4 5 [6] 7 8 ... 795