Commit d9a22835 authored by guillaume's avatar guillaume

world to screen transformation

parent 159ce2ba
#include "controlpanel.h"
#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
......@@ -18,13 +21,64 @@ void Panel_delete(Panel * self)
void Panel_initialize(Panel * self, NetWorld * world)
{
self->world= world;
self->camerax= 0;
self->cameray= 0;
self->scale= 1;
self->camera.x= 0.f;
self->camera.y= 0.f;
self->screenCenter.x= 400.f;
self->screenCenter.y= 300.f;
self->scale= 10.f; //pixel per meters
}
// To String
void Panel_print(Panel * self)
{
printf("Panel camera(x-%f, y-%f, scale-%f)\n", self->camerax, self->cameray, self->scale);
printf( "Panel camera(x-%f, y-%f, scale-%f)\n",
self->camera.x,
self->camera.y,
self->scale);
}
// Rendering
void Panel_draw(Panel * self)
{
BeginDrawing();
ClearBackground(RAYWHITE);
for(int i= 0 ; i < self->world->size ; ++i )
{
Panel_drawNode( self, &(self->world->nodes[i]) );
}
Panel_drawBasis(self);
EndDrawing();
}
void Panel_drawBasis(Panel * self)
{
Vector2 screen10= {1.f, 0.f};
screen10= Panel_pixelFromPosition(self, &( screen10 ) );
Vector2 screen01= {0.f, 1.f};
screen01= Panel_pixelFromPosition(self, &( screen01 ) );
DrawCircleV( self->screenCenter, 4, BLUE );
DrawLineV( self->screenCenter, screen10, RED );
DrawLineV( self->screenCenter, screen01, BLUE );
}
void Panel_drawNode(Panel * self, Node * n)
{
Vector2 screenPosition= Panel_pixelFromPosition(self, &(n->position) );
DrawCircleV(screenPosition, 24, MAROON);
DrawCircleV(screenPosition, 20, RAYWHITE);
}
Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p)
{
Vector2 pixel= {
self->screenCenter.x + (p->x - self->camera.x ) * self->scale,
self->screenCenter.y - (p->y - self->camera.y ) * self->scale
};
return pixel;
}
Vector2 Panel_positionFromPixel(Panel * self, Vector2 * p)
{
Vector2 position= { p->x, p->y };
return position;
}
......@@ -4,8 +4,8 @@
#include "networld.h"
struct Str_Panel {
double camerax, cameray;
double scale;
Vector2 camera, screenCenter;
float scale; // meter per pixel
NetWorld * world;
};
......@@ -21,4 +21,11 @@ void Panel_initialize(Panel * self, NetWorld * world);
// To String
void Panel_print(Panel * self);
// Rendering
void Panel_draw(Panel * self);
void Panel_drawBasis(Panel * self);
void Panel_drawNode(Panel * self, Node * n);
Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p);
Vector2 Panel_positionFromPixel(Panel * self, Vector2 * p);
#endif //CONTROLPANEL_H
\ No newline at end of file
......@@ -16,11 +16,10 @@
// Program attributes
//-------------------
const int screenWidth = 800;
const int screenHeight = 450;
const int screenHeight = 600;
const int targetFPS = 60;
void game_update(NetWorld * world);
void game_draw(NetWorld * world);
// Game attributes
//-----------------
......@@ -32,9 +31,9 @@ int main(int nbArg, char ** arg)
//--------------------
game_end= false;
NetWorld * world= NetWorld_new(3);
NetWorld_initNodePosition( world, 0, 10.4, 12.8 );
NetWorld_initNodePosition( world, 1, 110.4, 52.8 );
NetWorld_initNodePosition( world, 2, 384.5, 422.2 );
NetWorld_initNodePosition( world, 0, 1.4f, 1.28f );
NetWorld_initNodePosition( world, 1, 11.0f, 32.8f );
NetWorld_initNodePosition( world, 2, 18.4f, -12.2f );
Panel * panel= Panel_new();
Panel_initialize(panel, world);
......@@ -50,12 +49,15 @@ int main(int nbArg, char ** arg)
NetWorld_print(world);
puts("world expected:\n[10.4, 12.8]\n[110.4, 52.8]\n[384.5, 422.2]");
Panel_print(panel);
Vector2 position= {1.f, 1.f};
position= Panel_pixelFromPosition( panel, &position );
printf("[%.2f,%.2f]\n", position.x, position.y);
// Main game loop
while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key
{
game_update(world);
game_draw(world);
Panel_draw(panel);
}
// proper closing
......@@ -70,15 +72,3 @@ void game_update(NetWorld * world)
{
}
void game_draw(NetWorld * world)
{
BeginDrawing();
ClearBackground(RAYWHITE);
for(int i= 0 ; i < world->size ; ++i )
{
Vector2 nodePosition= { world->nodes[i].x, world->nodes[i].y };
DrawCircleV(nodePosition, 24, MAROON);
}
EndDrawing();
}
......@@ -20,15 +20,15 @@ void NetWorld_delete(NetWorld * self)
// Initialization
void NetWorld_initNodePosition(
NetWorld * self, int iNode,
double x, double y)
float x, float y)
{
self->nodes[iNode].x= x;
self->nodes[iNode].y= y;
self->nodes[iNode].position.x= x;
self->nodes[iNode].position.y= y;
}
// To String
void NetWorld_print(NetWorld * self)
{
for(int i= 0 ; i < self->size ; ++i )
printf("[%lf, %lf]\n", self->nodes[i].x, self->nodes[i].y);
printf("[%.2f, %.2f]\n", self->nodes[i].position.x, self->nodes[i].position.y);
}
#ifndef NETWORLD_H
#define NETWORLD_H
#include "raylib.h"
struct Str_Node {
double x, y;
Vector2 position;
};
typedef struct Str_Node Node;
......@@ -17,7 +19,7 @@ NetWorld * NetWorld_new(int aSize);
void NetWorld_delete(NetWorld * self);
// Initialization
void NetWorld_initNodePosition(NetWorld * self, int iNode, double x, double y); // position must be an float[size][2] array...
void NetWorld_initNodePosition(NetWorld * self, int iNode, float x, float y); // position must be an float[size][2] array...
// To String
void NetWorld_print(NetWorld * self);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment