# HG changeset patch # User Markus Bröker # Date 1229187476 -3600 # Node ID 236f8f7470730ed40f7ba3fb7b99b8e358b3ecce # Parent 820ed7fb9314d3c2fba969058842c2b1b787619a pimp my code, a small xdemo committer: Markus Bröker diff --git a/pmc/Makefile b/pmc/Makefile new file mode 100644 --- /dev/null +++ b/pmc/Makefile @@ -0,0 +1,28 @@ +#!/bin/bash + +CC=g++ +LD=ld +CFLAGS=-Wall -O2 -ansi +LDFLAGS=-lSDL +INCLUDE=include +CONFIG=-DSTEP=2 +OBJECTS=pmc.o cube.o main.o + +.SUFFIXES: .cc + +.cc.o: + $(CC) -c $(CFLAGS) -I$(INCLUDE) $(CONFIG) $< + +all: pmc + + +pmc: $(OBJECTS) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@ + +.PHONY: clean + +clean: + rm -f *.[oae]; + rm -f *~; + rm -f pmc glcube + diff --git a/pmc/cube.cc b/pmc/cube.cc new file mode 100644 --- /dev/null +++ b/pmc/cube.cc @@ -0,0 +1,94 @@ +/*** + * + * $Id: cube.cc,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/cube.cc,v $ + * + */ + +#include +#include "../sdl.cc" + +extern SDL_Surface *screen; +extern unsigned long color; + +Cube::Cube (Vector p1, Vector p2, Vector p3, Vector p4, int h) +{ + 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; +} + +Cube::~Cube () +{ + for (int i = 0; i < 8; i++) + delete P[i]; +} + +void Cube::move (Vector location) +{ + for (int i = 0; i < 8; i++) { + P[i] = new Vector (*P[i] + location); + } +} + +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; + + 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); + + 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); + + 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); + + 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); +} diff --git a/pmc/include/cube.h b/pmc/include/cube.h new file mode 100644 --- /dev/null +++ b/pmc/include/cube.h @@ -0,0 +1,25 @@ +/*** + * + * $Id: cube.h,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/include/cube.h,v $ + */ + +#ifndef _CUBE_H +#define _CUBE_H + +#include + +using namespace pmc; + +class Cube:public Vector { + public: + Vector * P[8]; + int height; + + Cube (Vector p1, Vector p2, Vector p3, Vector p4, int h); + virtual ~ Cube (); + void show (); + void move (Vector); +}; + +#endif diff --git a/pmc/include/object.h b/pmc/include/object.h new file mode 100644 --- /dev/null +++ b/pmc/include/object.h @@ -0,0 +1,22 @@ +/*** + * + * $Id: object.h,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/include/object.h,v $ + */ + +#ifndef OBJECT_H +#define OBJECT_H + +#include +#include + +class Object { + protected: + std::string name; + + public: + virtual ~ Object () { + std::cout << "Freeing Object " << name << std::endl; + } +}; +#endif diff --git a/pmc/include/pmc.h b/pmc/include/pmc.h new file mode 100644 --- /dev/null +++ b/pmc/include/pmc.h @@ -0,0 +1,43 @@ +/*** + * + * $Id: pmc.h,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/include/pmc.h,v $ + */ + +#ifndef PMC_H +#define PMC_H + +#include +#include + +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 diff --git a/pmc/main.cc b/pmc/main.cc new file mode 100644 --- /dev/null +++ b/pmc/main.cc @@ -0,0 +1,107 @@ +/*** + * + * $Id: main.cc,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/main.cc,v $ + * + */ + +#include +#include + +unsigned long color; +SDL_Surface *screen; + +#ifndef STEP +#define STEP 1 +#endif + +int main (int argc, char **argv) +{ + + unsigned long red, black; + SDL_Event event; + + if (argc != 2) { + std::cout << "USAGE: " << argv[0] + << " height" << std::endl; + exit (0); + } + int h = atoi (argv[1]); + + if ((h < 10) || (h >= 360)) + h = 200; + + Vector p1 (0, 0, 0); + Vector p2 (0, h, 0); + Vector p3 (h, 0, 0); + Vector p4 (h, h, 0); + + 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); + + 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)); + + bool running = true; + int x, y, z; + + x = y = z = 0; + + while (running) { + SDL_PollEvent (&event); + + if (event.type == SDL_QUIT) + running = false; + + if (event.type == SDL_KEYDOWN) { + color = black; + c->show (); + d->show (); + color = red; + + switch (event.key.keysym.sym) { + case SDLK_ESCAPE: + running = false; + break; + + case SDLK_UP: + c->move ((Vector (x, y - STEP, z))); + d->move ((Vector (x, y - STEP, z))); + break; + + case SDLK_DOWN: + c->move ((Vector (x, y + STEP, z))); + d->move ((Vector (x, y + STEP, z))); + break; + + case SDLK_LEFT: + c->move ((Vector (x - STEP, y, z))); + d->move ((Vector (x - STEP, y, z))); + break; + + case SDLK_RIGHT: + c->move ((Vector (x + STEP, y, z))); + d->move ((Vector (x + STEP, y, z))); + break; + default: + break; + } + + c->show (); + d->show (); + SDL_Flip (screen); + } + } + + delete c; + delete d; + + SDL_Quit (); + + return 0; +} diff --git a/pmc/pmc.cc b/pmc/pmc.cc new file mode 100644 --- /dev/null +++ b/pmc/pmc.cc @@ -0,0 +1,78 @@ +/*** + * + * $Id: pmc.cc,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/pmc.cc,v $ + * + */ + +#include +#include +#include + +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; +} diff --git a/pmc/sdl.cc b/pmc/sdl.cc new file mode 100644 --- /dev/null +++ b/pmc/sdl.cc @@ -0,0 +1,76 @@ +/*** + * + * $Id: sdl.cc,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $ + * $Source: /development/cpp/pmc/sdl.cc,v $ + * + */ + +#include + +#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; + } + } +} diff --git a/xdemo.c b/xdemo.c --- a/xdemo.c +++ b/xdemo.c @@ -21,7 +21,7 @@ } XSynchronize (dpy, 1); - w = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy), 0, 0, 640, 480, 1, 1, 1); + w = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy), 0, 0, 240, 60, 1, 1, 1); if (w < 0) return EXIT_SUCCESS; @@ -31,7 +31,7 @@ XRaiseWindow (dpy, w); XMapWindow (dpy, w); - printf ("Press any key to quit\n"); + printf ("Press ESC to quit\n"); active = 1; XSelectInput (dpy, w, KeyPressMask); @@ -40,7 +40,9 @@ XNextEvent (dpy, &xev); switch (xev.type) { case KeyPress: - active = 0; + if(xev.xkey.keycode == 9) + active = 0; + printf("Keycode = %2u\n", xev.xkey.keycode); break; default: printf ("Unknown Event: %d\n", xev.type);