Mail Archives: djgpp/1999/02/22/09:17:29
hi, i have a question regarding scaling in allegro. how do i make an
"object" shrink or stretch while keeping it at its original position?
for example, in the demo program below, the plane will shrink and grow,
but because the coordinates are multiplied by 1.1 / 0.9, it also moves.
how can i keep it from moving?
/* Fragments of C++ code within. Compile with "gxx" */
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <allegro.h>
#define MAXPOINTS 50
#define XSTEP 10
#define YSTEP 10
#define SHRINK 0.9
#define STRETCH 1.1
#define TRANSLATEMODE 0
#define SCALEMODE 1
typedef struct object {
int NumOfVerts;
float Coord[MAXPOINTS][2];
} Object;
void LoadObj(Object *);
void TransformObj(int, float, float, Object *);
void DrawObj(Object *);
void main() {
int Done = 0, Action;
Object MyObject;
allegro_init();
install_keyboard();
LoadObj(&MyObject);
if (!set_gfx_mode(GFX_AUTODETECT, 640, 400, 0, 0)) {
while (!Done) {
DrawObj(&MyObject);
Action = readkey() >> 8;
if (Action != KEY_ESC) {
switch(Action) {
case KEY_LEFT:
//TransformObj(TRANSLATEMODE, -XSTEP, 0,
&MyObject);
TransformObj(SCALEMODE, SHRINK, 1, &MyObject);
break;
case KEY_RIGHT:
//TransformObj(TRANSLATEMODE, XSTEP, 0,
&MyObject);
TransformObj(SCALEMODE, STRETCH, 1, &MyObject);
break;
case KEY_UP:
//TransformObj(TRANSLATEMODE, 0, -YSTEP,
&MyObject);
TransformObj(SCALEMODE, 1, SHRINK, &MyObject);
break;
case KEY_DOWN:
//TransformObj(TRANSLATEMODE, 0, YSTEP,
&MyObject);
TransformObj(SCALEMODE, 1, STRETCH, &MyObject);
break;
default:
break;
}
clear(screen);
}
else {
Done = 1;
}
}
}
else {
printf("Graphics init failed! Press any key to quit ...");
readkey();
}
}
void LoadObj(Object *MyObject) {
ifstream infile("d3.dat");
int counter;
infile >> MyObject->NumOfVerts;
for (counter = 0; counter < (MyObject->NumOfVerts * 2); counter++) {
infile >> MyObject->Coord[counter][0];
infile >> MyObject->Coord[counter][1];
}
infile.close();
}
void TransformObj(int mode, float x, float y, Object *MyObject) {
int counter;
float dummy; // use to hold useless z coordinate output, since 2d
(x, y) involve only
MATRIX_f TransMatrix; // Transformation Matrix
switch(mode) {
case 0:
get_translation_matrix_f(&TransMatrix, x, y, 0);
break;
case 1:
get_scaling_matrix_f(&TransMatrix, x, y, 1);
break;
}
for (counter = 0; counter < MyObject->NumOfVerts; counter++) {
apply_matrix_f(&TransMatrix,
MyObject->Coord[counter][0],
MyObject->Coord[counter][1], 0,
&MyObject->Coord[counter][0],
&MyObject->Coord[counter][1], &dummy);
}
}
void DrawObj(Object *MyObject) {
int counter, temp[50];
for (counter = 0; counter < MyObject->NumOfVerts; counter++) {
temp[counter * 2] = (float) MyObject->Coord[counter][0];
temp[counter * 2 + 1] = (float) MyObject->Coord[counter][1];
}
polygon(screen, MyObject->NumOfVerts, temp, 2);
}
--
regards,
lee.
===========================================================
figure out what to remove in email address. i tire of spam.
===========================================================
Suppose you were an idiot. And suppose you were a member of
Congress. But I repeat myself. -- Mark Twain
===========================================================
- Raw text -