Skip to content

Commit 3b66268

Browse files
committed
make sprites real vars, adapt code and implement MDT_ENTITY_Draw
1 parent 3ba6f67 commit 3b66268

File tree

6 files changed

+486
-502
lines changed

6 files changed

+486
-502
lines changed

examples/assets/sprites.c

Lines changed: 393 additions & 440 deletions
Large diffs are not rendered by default.

examples/example_mario.c

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
#include "mdt/examples/assets/sprites.h"
2424

2525

26+
static void DrawPipe(int x, int y, uint16_t height) {
27+
28+
MDT_DrawBitmap(&mario_pipe_spr0, x, y);
29+
30+
uint16_t i;
31+
for (i = 0; i < height; i++) {
32+
MDT_DrawBitmap(&mario_pipe_spr1, x, y + i + 11);
33+
}
34+
35+
}
36+
37+
2638
void MDT_EXAMPLE_mario(void) {
2739

2840
MDT_GRAPHICS_InitTypeDef graphicsCfg = {
@@ -38,15 +50,17 @@ void MDT_EXAMPLE_mario(void) {
3850

3951
MDT_USB_INPUT_Init();
4052

41-
MDT_BITMAP main_bmp;
42-
4353
uint8_t bgcolor = 0b00100111;
4454

4555
MDT_ENTITY mario;
4656
MDT_ENTITY_Init(&mario);
4757
mario.speed = 2;
4858
mario.x = 30;
4959
mario.y = 106;
60+
mario.sprites[0] = &mario_spr0;
61+
mario.sprites[1] = &mario_spr1;
62+
mario.sprites[2] = &mario_spr2;
63+
mario.sprites[3] = &mario_spr3;
5064

5165
uint32_t time = MDT_GetMs();
5266

@@ -68,52 +82,29 @@ void MDT_EXAMPLE_mario(void) {
6882
if (MDT_USB_INPUT_IsKbdKeyPressed(KEY_D)) right = 1; else right = 0;
6983
if (MDT_USB_INPUT_IsKbdKeyPressed(KEY_SPACEBAR)) MDT_ENTITY_Jump(&mario);
7084

71-
//if (MDT_GetMs() % 2000 == 0) MDT_ENTITY_Jump(&mario);
72-
7385
MDT_ENTITY_ProcessControl(&mario, 0, 0, left, right);
7486
MDT_ENTITY_ProcessPhysics(&mario, time_inc);
7587

7688
//Draw frame:
77-
MDT_GRAPHICS_GetBitmapFromContext(&main_bmp, &main_ctx);
7889
MDT_Clear(bgcolor);
7990

8091
REPORT_F("%d", (int) mario.y, 5, 15, 0x00);
8192
REPORT_F("%d", (int) mario.z, 5, 25, 0x00);
8293
REPORT_LF("Tick: %lu ms", MDT_GetMs(), 5, 5, 0x00);
8394
REPORT_LF("FPS: %hu", MDT_GetFPS(), 5, 35, 0x00);
8495

85-
DrawMario(&main_bmp, (int)mario.x, (int)(mario.y - mario.z), mario.spr);
86-
//MDT_ENTITY_Draw(&mario);
96+
MDT_ENTITY_Draw(&mario);
8797
for (uint8_t i = 0; i < 18; i++)
8898
for (uint8_t j = 0; j < 4; j++)
89-
DrawSuelo(&main_bmp, i * 17, 133 + j * 17);
99+
MDT_DrawBitmap(&mario_ground_spr, i * 17, 133 + j * 17);
90100

91-
DrawTuberia(&main_bmp, 110, 92, 30);
92-
DrawTuberia(&main_bmp, 210, 102, 20);
93-
DrawLogo(267, 0);
101+
DrawPipe(110, 92, 30);
102+
DrawPipe(210, 102, 20);
103+
MDT_DrawBitmap(&logo_spr, 267, 0);
94104

95105
MDT_WaitForVSync();
96106
MDT_SwapBuffers();
97107

98108
}
99109

100110
}
101-
102-
103-
/*
104-
105-
Old implementation reminders:
106-
107-
int frameid = AddFrame(150, 150, 150, 150, "super mario");
108-
MDT_BITMAP* mario_window = &(frames[frameid].canvas);
109-
strcat(console_text, "\niniciando demo super \nmario.");
110-
strcat(console_text, "\ntomando control del \npuerto serie.");
111-
strcat(console_text, "\npulsa intro para \ndevolver el control");
112-
113-
if (data == 'i') frames[frameid].y -= 3;
114-
if (data == 'k') frames[frameid].y += 3;
115-
if (data == 'j') frames[frameid].x -= 3;
116-
if (data == 'l') frames[frameid].x += 3;
117-
if (data == 0x0D) { data = 0; usart_captured = 0; strcat(console_text, "\ncontrol devuelto\n"); }
118-
119-
*/

examples/example_retro.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ void MDT_EXAMPLE_retro(void) {
119119
const uint16_t m_tick = tick % 500;
120120

121121
//if (m_tick >= 465 && m_tick < 469 || m_tick >= 474 && m_tick < 494 || m_tick >= 498 && m_tick < 500) {
122-
if (m_tick >= 466 && (tick % 2 == 0 || m_tick >= 482 && m_tick < 492)) {
123-
DrawCreature(110, 50);
122+
if (m_tick >= 466 && (tick % 2 == 0 || (m_tick >= 482 && m_tick < 492))) {
123+
//DrawCreature(110, 50);
124+
MDT_DrawBitmap(&creature_spr, 110, 50);
124125
}
125126

126127

examples/include/mdt/examples/assets/sprites.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,31 @@
33

44
#include "mdt/graphics.h"
55

6-
void DrawMario(MDT_BITMAP* bmp, int x, int y, int spr);
7-
void DrawSuelo(MDT_BITMAP* bmp, int x, int y);
8-
void DrawTuberia(MDT_BITMAP* bmp, int x, int y, int height);
9-
void DrawLogo(int x, int y);
10-
void DrawCreature(int x, int y);
6+
/**
7+
* ARCHIVO: "sprites.h"
8+
* NOMBRE: Sprites
9+
* AUTOR: José Carlos Hurtado
10+
*
11+
* Este archivo exporta los sprites que se utilizan en los ejemplos.
12+
*
13+
*/
14+
15+
#include "mdt/graphics.h"
16+
17+
18+
extern const MDT_BITMAP mario_spr0;
19+
extern const MDT_BITMAP mario_spr1;
20+
extern const MDT_BITMAP mario_spr2;
21+
extern const MDT_BITMAP mario_spr3;
22+
23+
extern const MDT_BITMAP mario_ground_spr;
24+
25+
extern const MDT_BITMAP mario_pipe_spr0;
26+
extern const MDT_BITMAP mario_pipe_spr1;
27+
28+
extern const MDT_BITMAP logo_spr;
29+
30+
extern const MDT_BITMAP creature_spr;
31+
1132

1233
#endif

include/mdt/ext/entity.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
#ifndef MDT_ENTITY_H
22
#define MDT_ENTITY_H
33

4+
#include "mdt/graphics.h"
5+
46
typedef struct {
57

6-
//TODO: add sprite assignment
7-
float speed_v, t_jump, spr_cntr;
8-
uint8_t jumping, spr;
8+
const MDT_BITMAP* sprites[16];
9+
//uint8_t jumping_spr_index;
10+
uint8_t spr;
11+
float spr_cntr;
912

1013
float force;
1114

1215
float force_angle;
1316

14-
float speed, max_speed, angle;
17+
uint8_t jumping;
18+
float speed;
19+
float angle;
20+
float max_speed;
21+
float speed_v;
1522
float x, y, z;
1623

1724
} MDT_ENTITY;

src/ext/entity.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*
1010
*/
1111

12-
#include "mdt/graphics.h"
1312
#include "mdt/ext/entity.h"
1413
#include <math.h>
1514
#include <stdbool.h>
@@ -22,7 +21,7 @@ void MDT_ENTITY_Init(MDT_ENTITY* ent) {
2221
ent->max_speed = 6;
2322
ent->x = ent->y = 270;
2423
//z es la coordenada vertical. lo hacemos así por que está pensado para juegos 2D
25-
ent->t_jump = ent->z = ent->speed = ent->speed_v = ent->angle = 0;
24+
ent->z = ent->speed = ent->speed_v = ent->angle = 0;
2625
ent->spr = 0;
2726
ent->spr_cntr = 0;
2827

@@ -35,28 +34,15 @@ void MDT_ENTITY_ProcessPhysics(MDT_ENTITY* ent, float time_inc) {
3534
ent->y -= ent->speed * sin(ent->angle) * time_inc * PPM;
3635

3736
if (ent->jumping) {
37+
3838
ent->speed_v -= 9.8 * time_inc * 2.9;
3939
ent->z += ent->speed_v * time_inc * PPM;
40-
//ent->t_jump += time_inc;
40+
4141
if (ent->z < 0) {
4242
ent->z = 0;
4343
ent->jumping = false;
44-
//ent->t_jump = 0;
45-
ent->spr = 0;
46-
}
47-
} else {
48-
//calculo del sprite:
49-
if (ent->speed != 0) {
50-
ent->spr_cntr += ent->speed / 5;
51-
if (ent->spr_cntr > 4) {
52-
ent->spr++;
53-
ent->spr_cntr = 0;
54-
}
55-
if (ent->spr > 2)
56-
ent->spr = 0;
57-
} else {
58-
ent->spr = 0;
5944
}
45+
6046
}
6147

6248
if (ent->speed > 0)
@@ -72,6 +58,7 @@ void MDT_ENTITY_ProcessPhysics(MDT_ENTITY* ent, float time_inc) {
7258
if (ent->speed > ent->max_speed) ent->speed = ent->max_speed;
7359

7460
ent->angle = atan2(speed_y, speed_x);
61+
7562
}
7663

7764

@@ -95,7 +82,6 @@ void MDT_ENTITY_Jump(MDT_ENTITY* ent) {
9582
if (ent->jumping) {
9683
return;
9784
}
98-
//t_jump = 0;
9985
ent->speed_v = 12.8;
10086
ent->jumping = true;
10187
ent->spr = 3;//dibujo saltando
@@ -106,7 +92,32 @@ void MDT_ENTITY_Jump(MDT_ENTITY* ent) {
10692
void MDT_ENTITY_Draw(MDT_ENTITY* ent) {
10793

10894
//draw_filled_ellipse(x, y - 4, 30, 8, GREY);//shadow
109-
//draw_filled_circle(x, y - z, 17, WHITE);
110-
//draw_bitmap(ent->x, ent->y - ent->z, ent->spr);
95+
//draw_filled_circle(x, y - z, 17, WHITE);//ball
96+
97+
const MDT_BITMAP* spr;
98+
99+
if (ent->jumping) {
100+
101+
spr = ent->sprites[3];
102+
103+
} else {
104+
105+
if (ent->speed != 0) {
106+
ent->spr_cntr += ent->speed / 5;
107+
if (ent->spr_cntr > 4) {
108+
ent->spr++;
109+
ent->spr_cntr = 0;
110+
}
111+
if (ent->spr > 2)
112+
ent->spr = 0;
113+
} else {
114+
ent->spr = 0;
115+
}
116+
117+
spr = ent->sprites[ent->spr];
118+
119+
}
120+
121+
MDT_DrawBitmap(spr, (int) ent->x, (int) (ent->y - ent->z));
111122

112123
}

0 commit comments

Comments
 (0)