Klassenhierarchie erneuert, Codebase erneuert
* Object -> Drawable -> ...
* Surface stellt eine SDL Schnittstelle bereit
* namespace pmc wurde aufgegeben
* DEBUG Option im Makefile
Bekannte Fehler:
* Jedes Drawable muss wissen und checken, ob es gezeichnet werden kann oder nicht
* Diese *P[i] = *P[i] + location Konstrukte machen mich irgendwie nervös...
-> operator* entfernt...
committer: Markus Bröker <mbroeker@largo.homelinux.org>
--- a/pmc/Makefile
+++ b/pmc/Makefile
@@ -1,28 +1,26 @@
-#!/bin/bash
-
-CC=g++
-LD=ld
-CFLAGS=-Wall -O2 -ansi
+CC=g++ -g -ggdb
+CFLAGS=-Wall -O2 -ansi
LDFLAGS=-lSDL
INCLUDE=include
-CONFIG=-DSTEP=2
-OBJECTS=pmc.o cube.o main.o
+CONFIG=-DSTEP=10
+
+OBJECTS=object.o surface.o vector.o rectangle.o cube.o main.o
+TARGET=pmc
.SUFFIXES: .cc
-.cc.o:
+.cc.o:
$(CC) -c $(CFLAGS) -I$(INCLUDE) $(CONFIG) $<
-all: pmc
+all: $(TARGET)
-pmc: $(OBJECTS)
+$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@
.PHONY: clean
clean:
rm -f *.[oae];
- rm -f *~;
- rm -f pmc glcube
-
+ rm -f *~ include/*~
+ rm -f $(TARGET)
--- a/pmc/cube.cc
+++ b/pmc/cube.cc
@@ -4,30 +4,48 @@
*/
#include <cube.h>
-#include "../sdl.cc"
+#include <cstdio>
+
+Cube::Cube (Surface * s)
+{
+ P[0] = new Vector (0, 0);
+ P[1] = new Vector (100, 0);
+ P[2] = new Vector (100, 100);
+ P[3] = new Vector (0, 100);
-extern SDL_Surface *screen;
-extern unsigned long color;
+ P[4] = new Vector (25, 25);
+ P[5] = new Vector (125, 25);
+ P[6] = new Vector (125, 125);
+ P[7] = new Vector (25, 125);
-Cube::Cube (Vector p1, Vector p2, Vector p3, Vector p4, int h)
+ surface = s;
+ height = 25;
+}
+
+Cube::Cube (Surface * s, Vector p1, Vector p2, Vector p3, Vector p4, int h)
{
+ height = h;
+ surface = s;
+
P[0] = new Vector (p1);
P[1] = new Vector (p2);
P[2] = new Vector (p3);
P[3] = new Vector (p4);
- P[4] = new Vector (p1.X (), p1.Y (), p1.Z () + h);
- P[5] = new Vector (p2.X (), p2.Y (), p2.Z () + h);
- P[6] = new Vector (p3.X (), p3.Y (), p3.Z () + h);
- P[7] = new Vector (p4.X (), p4.Y (), p4.Z () + h);
-
- height = h;
+ for (int i = 0; i < 4; i++) {
+ P[4 + i] = new Vector (*P[i]);
+ *P[4 + i] = *P[i] + Vector (h / 2, h / 2);
+ }
}
Cube::~Cube ()
{
- for (int i = 0; i < 8; i++)
+ for (int i = 0; i < 6; i++) {
+#ifdef DEBUG
+ fprintf (stderr, "Removing P[%d]: ", i);
+#endif
delete P[i];
+ }
}
void Cube::move (Vector location)
@@ -39,54 +57,22 @@
void Cube::show ()
{
- P[0]->vector ();
- std::cout << ", ";
- P[1]->vector ();
- std::cout << std::endl;
- P[2]->vector ();
- std::cout << ", ";
- P[3]->vector ();
- std::cout << std::endl;
+ /* Implement some Sanity Checks */
+ int height = surface->getHeight ();
- P[4]->vector ();
- std::cout << ", ";
- P[5]->vector ();
- std::cout << std::endl;
- P[6]->vector ();
- std::cout << ", ";
- P[7]->vector ();
- std::cout << std::endl;
-
- drawLine (screen, P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y (), color);
- drawLine (screen, P[2]->X (), P[2]->Y (), P[3]->X (), P[3]->Y (), color);
- drawLine (screen, P[0]->X (), P[0]->Y (), P[2]->X (), P[2]->Y (), color);
- drawLine (screen, P[1]->X (), P[1]->Y (), P[3]->X (), P[3]->Y (), color);
+ surface->drawLine (P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y ());
+ surface->drawLine (P[3]->X (), P[3]->Y (), P[2]->X (), P[2]->Y ());
+ surface->drawLine (P[2]->X (), P[2]->Y (), P[1]->X (), P[1]->Y ());
+ surface->drawLine (P[3]->X (), P[3]->Y (), P[0]->X (), P[0]->Y ());
- drawLine (screen, (int)(height / 2.0) + P[4]->X (),
- (int)(height / 2.0) + P[4]->Y (),
- (int)(height / 2.0) + P[5]->X (), (int)(height / 2.0) + P[5]->Y (), color);
-
- drawLine (screen, (int)(height / 2.0) + P[6]->X (),
- (int)(height / 2.0) + P[6]->Y (),
- (int)(height / 2.0) + P[7]->X (), (int)(height / 2.0) + P[7]->Y (), color);
-
- drawLine (screen, (int)(height / 2.0) + P[4]->X (),
- (int)(height / 2.0) + P[4]->Y (),
- (int)(height / 2.0) + P[6]->X (), (int)(height / 2.0) + P[6]->Y (), color);
+ surface->drawLine (P[4]->X (), P[4]->Y (), P[5]->X (), P[5]->Y ());
+ surface->drawLine (P[7]->X (), P[7]->Y (), P[6]->X (), P[6]->Y ());
+ surface->drawLine (P[7]->X (), P[7]->Y (), P[4]->X (), P[4]->Y ());
+ surface->drawLine (P[6]->X (), P[6]->Y (), P[5]->X (), P[5]->Y ());
- drawLine (screen, (int)(height / 2.0) + P[5]->X (),
- (int)(height / 2.0) + P[5]->Y (),
- (int)(height / 2.0) + P[7]->X (), (int)(height / 2.0) + P[7]->Y (), color);
-
- drawLine (screen, P[0]->X (), P[0]->Y (),
- (int)(height / 2.0) + P[4]->X (), (int)(height / 2.0) + P[4]->Y (), color);
+ surface->drawLine (P[0]->X (), P[0]->Y (), P[4]->X (), P[4]->Y ());
+ surface->drawLine (P[1]->X (), P[1]->Y (), P[5]->X (), P[5]->Y ());
- drawLine (screen, P[2]->X (), P[2]->Y (),
- (int)(height / 2.0) + P[6]->X (), (int)(height / 2.0) + P[6]->Y (), color);
-
- drawLine (screen, P[1]->X (), P[1]->Y (),
- (int)(height / 2.0) + P[5]->X (), (int)(height / 2.0) + P[5]->Y (), color);
-
- drawLine (screen, P[3]->X (), P[3]->Y (),
- (int)(height / 2.0) + P[7]->X (), (int)(height / 2.0) + P[7]->Y (), color);
+ surface->drawLine (P[2]->X (), P[2]->Y (), P[6]->X (), P[6]->Y ());
+ surface->drawLine (P[3]->X (), P[3]->Y (), P[7]->X (), P[7]->Y ());
}
--- a/pmc/include/cube.h
+++ b/pmc/include/cube.h
@@ -6,16 +6,19 @@
#ifndef _CUBE_H
#define _CUBE_H
-#include <pmc.h>
+#include <surface.h>
+#include <vector.h>
+#include <rectangle.h>
-using namespace pmc;
-
-class Cube:public Vector {
- public:
+class Cube:public Drawable {
+ private:
Vector * P[8];
int height;
- Cube (Vector p1, Vector p2, Vector p3, Vector p4, int h);
+ public:
+ Cube (Surface *, Vector, Vector, Vector, Vector, int);
+ Cube (Surface *);
+
virtual ~ Cube ();
void show ();
void move (Vector);
new file mode 100644
--- /dev/null
+++ b/pmc/include/drawable.h
@@ -0,0 +1,22 @@
+/**
+ * test/demos/pmc/include/drawable.h
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#ifndef DRAWABLE_H
+#define DRAWABLE_H
+
+#include <surface.h>
+#include <vector.h>
+
+class Drawable:public Object {
+ protected:
+ Vector anker;
+ Surface *surface;
+
+ public:
+ virtual ~ Drawable () {};
+ virtual void move (Vector location) = 0;
+ virtual void show () = 0;
+};
+#endif
--- a/pmc/include/object.h
+++ b/pmc/include/object.h
@@ -7,15 +7,14 @@
#define OBJECT_H
#include <string>
-#include <iostream>
class Object {
protected:
std::string name;
public:
- virtual ~ Object () {
- std::cout << "Freeing Object " << name << std::endl;
- };
+ virtual ~ Object ();
+ virtual std::string getName ();
+ virtual Object getClass ();
};
#endif
deleted file mode 100644
--- a/pmc/include/pmc.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * test/demos/pmc/include/pmc.h
- * Copyright (C) 2008 Markus Broeker
- */
-
-#ifndef PMC_H
-#define PMC_H
-
-#include <iostream>
-#include <object.h>
-
-namespace pmc {
- class Vector:public Object {
- private:
- int x;
- int y;
- int z;
-
- public:
- enum Mode { DEG, RAD, GRAD };
-
- Vector (int xx = 0, int yy = 0, int zz = 0);
- virtual ~ Vector ();
-
- int X ();
- int Y ();
- int Z ();
-
- Vector operator+ (Vector);
- Vector operator- (Vector);
- int operator* (Vector);
- double abs ();
-
- void vector ();
- double angle (Vector);
- void setMode (Mode m = RAD);
-
- private:
- Mode mode;
- };
-}
-#endif
new file mode 100644
--- /dev/null
+++ b/pmc/include/rectangle.h
@@ -0,0 +1,24 @@
+/**
+ * test/demos/pmc/include/rectangle.h
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#ifndef RECTANGLE_H
+#define RECTANGLE_H
+
+#include <surface.h>
+#include <vector.h>
+#include <drawable.h>
+
+class Rectangle:public Drawable {
+ private:
+ Vector * P[4];
+
+ public:
+ Rectangle (Surface *, Vector p1, Vector p2, Vector p3, Vector p4);
+ Rectangle (Surface *);
+ virtual ~ Rectangle ();
+ void move (Vector);
+ void show ();
+};
+#endif
new file mode 100644
--- /dev/null
+++ b/pmc/include/surface.h
@@ -0,0 +1,39 @@
+/**
+ * test/demos/pmc/include/surface.h
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#ifndef SURFACE_H
+#define SURFACE_H
+
+#include <object.h>
+#include <SDL/SDL.h>
+
+class Surface:public Object {
+ private:
+ int width;
+ int height;
+ int depth;
+
+ Uint32 color, red, black;
+ SDL_Surface *screen;
+
+ public:
+ enum foregroundColor { BLACK, RED };
+ Surface (int w, int h, int d);
+ ~Surface ();
+
+ int getWidth () {
+ return width;
+ };
+ int getHeight () {
+ return height;
+ };
+
+ void drawPixel (int x, int y);
+ void drawLine (int x1, int y1, int x2, int y2);
+
+ void flip ();
+ void setColor (enum foregroundColor);
+};
+#endif
new file mode 100644
--- /dev/null
+++ b/pmc/include/vector.h
@@ -0,0 +1,34 @@
+/**
+ * test/demos/pmc/include/vector.h
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include <object.h>
+
+class Vector:public Object {
+ public:
+ enum Mode { DEG, RAD, GRAD };
+
+ Vector (int xx = 0, int yy = 0);
+ virtual ~ Vector ();
+
+ int X ();
+ int Y ();
+
+ Vector operator+ (Vector);
+ Vector operator- (Vector);
+ double abs ();
+
+ void vector ();
+ double angle (Vector);
+ void setMode (Mode m = RAD);
+
+ private:
+ Mode mode;
+ int x;
+ int y;
+};
+#endif
--- a/pmc/main.cc
+++ b/pmc/main.cc
@@ -3,52 +3,54 @@
* Copyright (C) 2008 Markus Broeker
*/
+#include <surface.h>
+#include <rectangle.h>
#include <cube.h>
-#include <SDL/SDL.h>
-unsigned long color;
-SDL_Surface *screen;
+#include <cstdio>
+#include <cstdlib>
#ifndef STEP
#define STEP 1
#endif
+#define MAX 3
+
int main (int argc, char **argv)
{
+ Surface *surface;
+ SDL_Event event;
+ Drawable *d[MAX];
- unsigned long red, black;
- SDL_Event event;
+ int height;
if (argc != 2) {
- std::cout << "USAGE: " << argv[0]
- << " height" << std::endl;
- exit (0);
- }
- int h = atoi (argv[1]);
+ fprintf (stderr, "Usage: %s <HEIGHT>\n", argv[0]);
+ height = 250;
+ } else
+ height = atoi (argv[1]);
- if ((h < 10) || (h >= 360))
- h = 200;
+ if ((height < 10) || (height >= 360))
+ height = 200;
- Vector p1 (0, 0, 0);
- Vector p2 (0, h, 0);
- Vector p3 (h, 0, 0);
- Vector p4 (h, h, 0);
+ Vector p1 (0, 0);
+ Vector p2 (height, 0);
+ Vector p3 (height, height);
+ Vector p4 (0, height);
- SDL_Init (SDL_INIT_VIDEO);
- screen = SDL_SetVideoMode (1010, 700, 16, SDL_HWSURFACE);
- red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
- black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
+ surface = new Surface (1024, 768, 16);
- Cube *c = new Cube (p1, p2, p3, p4, h);
- Cube *d = new Cube (p1, p2, p3, p4, h);
-
- c->move (Vector (100, 250, 0));
- d->move (Vector (400, 250, 0));
+ d[0] = new Rectangle (surface, p1, p2, p3, p4);
+ d[1] = new Cube (surface);
+ d[2] = new Cube (surface, p1, p2, p3, p4, height);
bool running = true;
- int x, y, z;
+ int x, y;
- x = y = z = 0;
+ x = y = 0;
+
+ d[1]->move (Vector (25, 25));
+ d[2]->move (Vector (290, 0));
while (running) {
SDL_PollEvent (&event);
@@ -57,10 +59,10 @@
running = false;
if (event.type == SDL_KEYDOWN) {
- color = black;
- c->show ();
- d->show ();
- color = red;
+ surface->setColor (Surface::BLACK);
+ for (int i = 0; i < MAX; i++)
+ d[i]->show ();
+ surface->setColor (Surface::RED);
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
@@ -68,38 +70,40 @@
break;
case SDLK_UP:
- c->move ((Vector (x, y - STEP, z)));
- d->move ((Vector (x, y - STEP, z)));
+ for (int i = 0; i < MAX; i++)
+ d[i]->move ((Vector (x, y - STEP)));
break;
case SDLK_DOWN:
- c->move ((Vector (x, y + STEP, z)));
- d->move ((Vector (x, y + STEP, z)));
+ for (int i = 0; i < MAX; i++)
+ d[i]->move ((Vector (x, y + STEP)));
break;
case SDLK_LEFT:
- c->move ((Vector (x - STEP, y, z)));
- d->move ((Vector (x - STEP, y, z)));
+ for (int i = 0; i < MAX; i++)
+ d[i]->move ((Vector (x - STEP, y)));
break;
case SDLK_RIGHT:
- c->move ((Vector (x + STEP, y, z)));
- d->move ((Vector (x + STEP, y, z)));
+ for (int i = 0; i < MAX; i++)
+ d[i]->move ((Vector (x + STEP, y)));
break;
default:
break;
}
- c->show ();
- d->show ();
- SDL_Flip (screen);
+ for (int i = 0; i < MAX; i++) {
+ d[i]->show ();
+ }
+
+ surface->flip ();
}
}
- delete c;
- delete d;
+ for (int i = 0; i < MAX; i++)
+ delete d[i];
- SDL_Quit ();
+ delete surface;
- return 0;
+ return EXIT_SUCCESS;
}
new file mode 100644
--- /dev/null
+++ b/pmc/object.cc
@@ -0,0 +1,25 @@
+/**
+ * test/demos/pmc/object.cc
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <object.h>
+
+#include <iostream>
+
+Object::~Object ()
+{
+#ifdef DEBUG
+ std::cerr << "Freeing Object " << name << std::endl;
+#endif
+}
+
+std::string Object::getName ()
+{
+ return name;
+}
+
+Object Object::getClass ()
+{
+ return *this;
+}
deleted file mode 100644
--- a/pmc/pmc.cc
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * test/demos/pmc/pmc.cc
- * Copyright (C) 2008 Markus Broeker
- */
-
-#include <iostream>
-#include <cmath>
-#include <pmc.h>
-
-using namespace pmc;
-
-Vector::Vector (int xx, int yy, int zz)
-{
- name = "Vector";
- x = xx;
- y = yy;
- z = zz;
- mode = RAD;
-}
-
-Vector::~Vector ()
-{
-}
-
-int Vector::X ()
-{
- return x;
-}
-
-int Vector::Y ()
-{
- return y;
-}
-
-int Vector::Z ()
-{
- return z;
-}
-
-Vector Vector::operator+ (Vector a)
-{
- return Vector (x + a.X (), y + a.Y (), z + a.Z ());
-}
-
-Vector Vector::operator- (Vector a)
-{
- return Vector (x - a.X (), y - a.Y (), z - a.Z ());
-}
-
-int Vector::operator* (Vector a)
-{
- return (x * a.X () + y * a.Y () + z * a.Z ());
-}
-
-double Vector::abs ()
-{
- return (std::sqrt (x * x + y * y + z * z));
-}
-
-void Vector::vector ()
-{
- std::cout << "(" << x << ", " << y << ", " << z << ")";
-}
-
-double Vector::angle (Vector p)
-{
- if (mode == DEG)
- return ((180.0 / M_PI) * std::acos ((p ** this) / (abs () * p.abs ())));
-
- return (std::acos ((p ** this) / (abs () * p.abs ())));
-}
-
-void Vector::setMode (Mode m)
-{
- mode = m;
-}
new file mode 100644
--- /dev/null
+++ b/pmc/rectangle.cc
@@ -0,0 +1,80 @@
+/**
+ * test/demos/pmc/rectangle.cc
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <rectangle.h>
+#include <cstdio>
+
+Rectangle::Rectangle (Surface * s)
+{
+ P[0] = new Vector (0, 0);
+ P[1] = new Vector (0, 100);
+ P[2] = new Vector (100, 100);
+ P[3] = new Vector (0, 100);
+
+ anker = Vector (0, 0);
+
+ surface = s;
+}
+
+Rectangle::Rectangle (Surface * s, Vector p1, Vector p2, Vector p3, Vector p4)
+{
+ P[0] = new Vector (p1);
+ P[1] = new Vector (p2);
+ P[2] = new Vector (p3);
+ P[3] = new Vector (p4);
+
+ anker = Vector (0, 0);
+
+ surface = s;
+}
+
+Rectangle::~Rectangle ()
+{
+ for (int i = 0; i < 4; i++) {
+#ifdef DEBUG
+ fprintf (stderr, "Removing P[%d]: ", i);
+#endif
+ delete P[i];
+ }
+}
+
+void Rectangle::move (Vector location)
+{
+ if ((anker.X () + location.X ()) < 0)
+ return;
+ if ((anker.Y () + location.Y ()) < 0)
+ return;
+
+ if ((anker.X () + location.X ()) >= surface->getWidth ())
+ return;
+ if ((anker.Y () + location.Y ()) >= surface->getHeight ())
+ return;
+
+ for (int i = 0; i < 4; i++) {
+ *P[i] = *P[i] + location;
+ }
+
+ anker = Vector (P[0]->X (), P[0]->Y ());
+}
+
+void Rectangle::show ()
+{
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ fprintf (stderr, "[%d] ", i);
+ P[i]->vector ();
+ if ((i + 1) % 4 != 0)
+ fprintf (stderr, ", ");
+ else
+ fprintf (stderr, "\n");
+ }
+
+ surface->drawLine (P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y ());
+ surface->drawLine (P[3]->X (), P[3]->Y (), P[2]->X (), P[2]->Y ());
+
+ surface->drawLine (P[2]->X (), P[2]->Y (), P[1]->X (), P[1]->Y ());
+ surface->drawLine (P[3]->X (), P[3]->Y (), P[0]->X (), P[0]->Y ());
+}
deleted file mode 100644
--- a/pmc/sdl.cc
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * test/demos/pmc/sdl.cc
- * Copyright (C) 2008 Markus Broeker
- */
-
-#include <SDL/SDL.h>
-
-#define max(a,b) (((a) > (b)) ? (a) : (b))
-#define min(a,b) (((a) < (b)) ? (a) : (b))
-#define abs(a) (((a)<0) ? -(a) : (a))
-#define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
-
-void drawLine (SDL_Surface * s, int x1, int y1, int x2, int y2, Uint32 color)
-{
- int d;
- int x;
- int y;
- int ax;
- int ay;
- int sx;
- int sy;
- int dx;
- int dy;
-
- Uint8 *lineAddr;
- Sint32 yOffset;
-
- dx = x2 - x1;
- ax = abs (dx) << 1;
- sx = sign (dx);
-
- dy = y2 - y1;
- ay = abs (dy) << 1;
- sy = sign (dy);
- yOffset = sy * s->pitch;
-
- x = x1;
- y = y1;
-
- lineAddr = ((Uint8 *) s->pixels) + (y * s->pitch);
- if (ax > ay) { /* x dominant */
- d = ay - (ax >> 1);
- for (;;) {
- *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
-
- if (x == x2) {
- return;
- }
- if (d >= 0) {
- y += sy;
- lineAddr += yOffset;
- d -= ax;
- }
- x += sx;
- d += ay;
- }
- } else { /* y dominant */
- d = ax - (ay >> 1);
- for (;;) {
- *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
-
- if (y == y2) {
- return;
- }
- if (d >= 0) {
- x += sx;
- d -= ay;
- }
- y += sy;
- lineAddr += yOffset;
- d += ax;
- }
- }
-}
new file mode 100644
--- /dev/null
+++ b/pmc/surface.cc
@@ -0,0 +1,174 @@
+/**
+ * test/demos/pmc/surface.cc
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <surface.h>
+
+#define max(a,b) (((a) > (b)) ? (a) : (b))
+#define min(a,b) (((a) < (b)) ? (a) : (b))
+#define abs(a) (((a)<0) ? -(a) : (a))
+#define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
+
+Surface::Surface (int w, int h, int d)
+{
+ width = w;
+ height = h;
+ depth = d;
+
+ SDL_Init (SDL_INIT_VIDEO);
+ screen = SDL_SetVideoMode (width, height, depth, SDL_HWSURFACE);
+ red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
+ black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
+
+ color = red;
+}
+
+Surface::~Surface ()
+{
+ SDL_Quit ();
+}
+
+void Surface::drawPixel (int x, int y)
+{
+ if (SDL_MUSTLOCK (screen)) {
+ if (SDL_LockSurface (screen) < 0) {
+ return;
+ }
+ }
+
+ switch (screen->format->BytesPerPixel) {
+ case 1:{ /* vermutlich 8 Bit */
+ Uint8 *bufp;
+
+ bufp = (Uint8 *) screen->pixels + y * screen->pitch + x;
+ *bufp = color;
+ }
+ break;
+
+ case 2:{ /* vermutlich 15 Bit oder 16 Bit */
+ Uint16 *bufp;
+
+ bufp = (Uint16 *) screen->pixels + y * screen->pitch / 2 + x;
+ *bufp = color;
+ }
+ break;
+
+ case 3:{ /* langsamer 24-Bit-Modus, selten verwendet */
+ Uint8 *bufp;
+
+ bufp = (Uint8 *) screen->pixels + y * screen->pitch + x * 3;
+ if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {
+ bufp[0] = color;
+ bufp[1] = color >> 8;
+ bufp[2] = color >> 16;
+ } else {
+ bufp[2] = color;
+ bufp[1] = color >> 8;
+ bufp[0] = color >> 16;
+ }
+ }
+ break;
+
+ case 4:{ /* vermutlich 32 Bit */
+ Uint32 *bufp;
+
+ bufp = (Uint32 *) screen->pixels + y * screen->pitch / 4 + x;
+ *bufp = color;
+ }
+ break;
+ }
+
+ if (SDL_MUSTLOCK (screen)) {
+ SDL_UnlockSurface (screen);
+ }
+}
+
+void Surface::drawLine (int x1, int y1, int x2, int y2)
+{
+ int d;
+ int x;
+ int y;
+ int ax;
+ int ay;
+ int sx;
+ int sy;
+ int dx;
+ int dy;
+
+ Uint8 *lineAddr;
+ Sint32 yOffset;
+
+ /*
+ * SANITY CHECK: fix segfault in *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
+ */
+ if (y1 < 0 || y2 < 0)
+ return;
+
+ if (x1 < 0 || x2 < 0)
+ return;
+
+ dx = x2 - x1;
+ ax = abs (dx) << 1;
+ sx = sign (dx);
+
+ dy = y2 - y1;
+ ay = abs (dy) << 1;
+ sy = sign (dy);
+ yOffset = sy * screen->pitch;
+
+ x = x1;
+ y = y1;
+
+ lineAddr = ((Uint8 *) screen->pixels) + (y * screen->pitch);
+ if (ax > ay) { /* x dominant */
+ d = ay - (ax >> 1);
+ for (;;) {
+ *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
+
+ if (x == x2) {
+ return;
+ }
+ if (d >= 0) {
+ y += sy;
+ lineAddr += yOffset;
+ d -= ax;
+ }
+ x += sx;
+ d += ay;
+ }
+ } else { /* y dominant */
+ d = ax - (ay >> 1);
+ for (;;) {
+ *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
+
+ if (y == y2) {
+ return;
+ }
+ if (d >= 0) {
+ x += sx;
+ d -= ay;
+ }
+ y += sy;
+ lineAddr += yOffset;
+ d += ax;
+ }
+ }
+}
+
+void Surface::flip ()
+{
+ SDL_Flip (screen);
+}
+
+void Surface::setColor (foregroundColor c)
+{
+ switch (c) {
+ case BLACK:
+ color = black;
+ break;
+ case RED:
+ color = red;
+ break;
+ };
+}
new file mode 100644
--- /dev/null
+++ b/pmc/vector.cc
@@ -0,0 +1,71 @@
+/**
+ * test/demos/pmc/vector.cc
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <cmath>
+#include <vector.h>
+
+#include <cstdio>
+
+Vector::Vector (int xx, int yy)
+{
+ name = "Vector";
+ x = xx;
+ y = yy;
+ mode = RAD;
+
+#ifdef DEBUG
+ fprintf (stderr, "New Vector: (%.3d, %.3d)\n", x, y);
+#endif
+}
+
+Vector::~Vector ()
+{
+#ifdef DEBUG
+ fprintf (stderr, "Bye, bye Vector: (%.3d, %.3d)\n", x, y);
+#endif
+}
+
+int Vector::X ()
+{
+ return x;
+}
+
+int Vector::Y ()
+{
+ return y;
+}
+
+Vector Vector::operator+ (Vector a)
+{
+ return Vector (x + a.X (), y + a.Y ());
+}
+
+Vector Vector::operator- (Vector a)
+{
+ return Vector (x - a.X (), y - a.Y ());
+}
+
+double Vector::abs ()
+{
+ return (std::sqrt (x * x + y * y));
+}
+
+void Vector::vector ()
+{
+ fprintf (stderr, "(%.3d, %.3d)", x, y);
+}
+
+double Vector::angle (Vector v)
+{
+ if (mode == DEG)
+ return ((180.0 / M_PI) * std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ())));
+
+ return (std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ())));
+}
+
+void Vector::setMode (Mode m)
+{
+ mode = m;
+}