-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBeginners_-_StructStackListFunction.c
99 lines (80 loc) · 3.39 KB
/
Beginners_-_StructStackListFunction.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Stack and List and other data structures are not build into the c language.
// If you need to convert code that has stacks or lists you need to write or re use code
// for that.
//
// As far as I have gotten to know Clang and sources about it you need to use
// arrays for these. A stack is a array with several functions to manipulate
// it. Most known are Pop And Push. With making you own stack from a array
// the Push adds the data to the highest location[0..highest] in the array. You actually
// need to keep track of this by a counter that holds the size of your stack. (You need to
// create arrays in Clang at program start and thus need to estimate how large they might
// need to be.)
// A pop removes the highest location from the array and decreases the counter with one.
//
// You can write more functions to remove items from the middle of the list or stack. You then
// need to shift or "sort" the array.
#define MAXSTACKSIZE 1024 // how large a stack do we need?
#include "raylib.h"
// Our stack
static int mystacklen=0;
typedef struct stack{
int x;
int y;
}stack;
static stack mystack[MAXSTACKSIZE]; // Here this stack is global to our program.
static void push(struct stack in);
static void pop(void);
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example.");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Create a struct called banana and pass it to the push function.
struct stack banana;
banana.x = 10;
banana.y = 20;
push(banana);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if(IsKeyReleased(KEY_SPACE)){
// Another way to create and pass a struct through a function.
struct stack banana={GetRandomValue(0,100),0};
push(banana);
}
if(IsKeyReleased(KEY_ENTER)){
pop();
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Press space to add to stack, enter to remove",200,10,20,DARKGRAY);
DrawText(FormatText("Stack size : %i",mystacklen),320,30,20,DARKGRAY);
for(int i=0;i<mystacklen;i++){
DrawText(FormatText("stack %i",mystack[i].x),0,20+i*20,20,BLACK);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
void pop(){
if(mystacklen>0)mystacklen-=1;
}
void push(struct stack in){
mystack[mystacklen] = in;
mystacklen+=1;
}