repl.it
@CullenDAvello/

legion repl V.02

C

No description

fork
loading
Files
  • main.c
  • PDL V.01.c
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "SDL.h"
#include "SDL_image.h"
#include <stdio.h>
#include <time.h>

typedef struct
{
  int x, y,
  short life;
  char *name;
}Man;

typedef struct
{
  int x, y;
} Star;

typedef struct
{
  Man man;

  Star stars[100];

  SDL_Texture *star;
  SDL_Renderer *renderer;
} GameState;

void loadGame(GameState *game)
{
SDL_Surface *starSurface = NULL;

    starSurface = IMG_Load("star.png");
  if(starSurface == NULL)
  {
    printf("Cannot find star.png!\n\n");
    SDL_Quit();
    exit(1);
  }

  gamestate.star = SDL_CreateTextureFromSurface(game->renderer, starSurface);
  SDL_FreeSurface(starSurface);

  game->man,x = 320-40;
  game->man.y = 240-40;

  for(int i = 0; i < 100; i++)
  {
    game->stars[i].x = random()%640;
    game->stars[i].y = random()%480;
  }
}

int processEvents(SDL_Window *window, GameState *game)
{
  SDL_EVENT event;
  int done = 0;

  while(SDL_PollEvent(&event))
  {
    switch(event.type)
    {
      case SDL_WINDOWEVENT_CLOSE:
      {
         if(window)
         {
           SDL_DestroyWindow(window);
           window = NULL;
           done = 1;
         }
      }
      break;
      case SDL_KEYDOWN:
      {
        switch(event.key.keysym.sym)
        {
          case SDLK_ESCAPE:
            done = 1;
          break;
        }
      }
      break;
      case SDL QUIT:
        done = 1;
      break;
    }
  }

  const Uint8 *state = SDL_GetKeyboardState(NULL);
  if (state[SDL_SCANCODE_LEFT])   
  {
    game->man.x -= 10;
  }
  if(state[SDL_SCANCODE_RIGHT])
  {
    game->man.x += 10;
  }
    if (state[SDL_SCANCODE_UP])   
  {
    game->man.y -= 10;
  }
  if(state[SDL_SCANCODE_DOWN])
  {
    game->man.y += 10;
  }
   
  return done;
}

void doRender(SDL_Renderer *renderer, Man *man)
{ 
  SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);

  SDL_RenderClear(renderer);

  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

  SDL_Rect rect = { game->man.x, game->man.y, 80, 80 };
  SDL_RenderFillRect(renderer, &rect);

  for(int 1 = 0; i < 100; i++)
  {
    SDL_Rect starRect = { game->stars[i].x, game->stars[i].y, 64, 64 };
    SDL_RenderCopy(renderer, game->star, NULL, &starRect)
  }
  SDL_RenderPresent(renderer);
}

int main (int argc, char *argv[])
{
  GameState gameState;
  SDL_Window *window = NULL;
  SDL_Renderer *renderer = NULL;
  

  SDL_Init(SDL_INIT_VIDEO);

  srandom((int)time(NULL);

  window = SDL_CreateWindow("Game Window",
                            SDL_WINDOWPOS_UNDEFINED,
                            SDL_WINDOWPOS_UNDEFINED,
                            640,
                            480,
                            0
                            );
  renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)
  gameState.renderer = renderer;

  loadGame(&gameState);

  int done = 0;

  while(!done)
  {
    done = processEvents(window, &man);

    doRenderer(renderer, &man)

    SDL_Delay(10);
  }  


  SDL_DestroyTexture(gameState.star)  

  SDL_DestroyWindow(window);
  SDL_DestroyRenderer(renderer);

  SDL_Quit();
  return 0;
}