Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
NetWorld
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LAIRD Timothy
NetWorld
Commits
d9a22835
Commit
d9a22835
authored
Oct 15, 2020
by
guillaume
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
world to screen transformation
parent
159ce2ba
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
31 deletions
+84
-31
controlpanel.c
src/controlpanel.c
+58
-4
controlpanel.h
src/controlpanel.h
+9
-2
main-viewer.c
src/main-viewer.c
+9
-19
networld.c
src/networld.c
+4
-4
networld.h
src/networld.h
+4
-2
No files found.
src/controlpanel.c
View file @
d9a22835
#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
;
}
src/controlpanel.h
View file @
d9a22835
...
...
@@ -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
src/main-viewer.c
View file @
d9a22835
...
...
@@ -16,11 +16,10 @@
// Program attributes
//-------------------
const
int
screenWidth
=
800
;
const
int
screenHeight
=
45
0
;
const
int
screenHeight
=
60
0
;
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
,
1
0
.
4
,
12
.
8
);
NetWorld_initNodePosition
(
world
,
1
,
11
0
.
4
,
52
.
8
);
NetWorld_initNodePosition
(
world
,
2
,
384
.
5
,
422
.
2
);
NetWorld_initNodePosition
(
world
,
0
,
1
.
4
f
,
1
.
28
f
);
NetWorld_initNodePosition
(
world
,
1
,
11
.
0
f
,
32
.
8
f
);
NetWorld_initNodePosition
(
world
,
2
,
18
.
4
f
,
-
12
.
2
f
);
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
();
}
src/networld.c
View file @
d9a22835
...
...
@@ -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
);
}
src/networld.h
View file @
d9a22835
#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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment