Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GAME INSA PROJECT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
3EII INFO LKCH
GAME INSA PROJECT
Commits
7b236910
Commit
7b236910
authored
4 years ago
by
Le-Bao-Tin.Ha
Browse files
Options
Downloads
Patches
Plain Diff
Update main.c
parent
2a9d7d23
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
GAME/main.c
+1
-191
1 addition, 191 deletions
GAME/main.c
with
1 addition
and
191 deletions
GAME/main.c
+
1
−
191
View file @
7b236910
...
...
@@ -11,195 +11,5 @@
int
main
(
int
argc
,
char
*
args
[])
{
printmap
();
// attempt to initialize graphics and timer system
if
(
SDL_Init
(
SDL_INIT_VIDEO
|
SDL_INIT_TIMER
)
!=
0
)
{
printf
(
"error initializing SDL: %s
\n
"
,
SDL_GetError
());
return
1
;
}
SDL_Window
*
win
=
SDL_CreateWindow
(
"INSAGAME"
,
SDL_WINDOWPOS_CENTERED
,
SDL_WINDOWPOS_CENTERED
,
WINDOW_WIDTH
,
WINDOW_HEIGHT
,
0
);
if
(
!
win
)
{
printf
(
"error creating window: %s
\n
"
,
SDL_GetError
());
SDL_Quit
();
return
1
;
}
// create a renderer, which sets up the graphics hardware
Uint32
render_flags
=
SDL_RENDERER_ACCELERATED
;
SDL_Renderer
*
rend
=
SDL_CreateRenderer
(
win
,
-
1
,
render_flags
);
if
(
!
rend
)
{
printf
(
"error creating renderer: %s
\n
"
,
SDL_GetError
());
SDL_DestroyWindow
(
win
);
SDL_Quit
();
return
1
;
}
// load the background into memory using SDL_image library function
SDL_Surface
*
background
=
IMG_Load
(
"ressource/map (2).bmp"
);
if
(
!
background
)
{
printf
(
"error creating surface
\n
"
);
SDL_DestroyRenderer
(
rend
);
SDL_DestroyWindow
(
win
);
SDL_Quit
();
return
1
;
}
// load the image data into the graphics hardware's memory
SDL_Texture
*
tex
=
SDL_CreateTextureFromSurface
(
rend
,
background
);
SDL_FreeSurface
(
background
);
if
(
!
tex
)
{
printf
(
"error creating texture: %s
\n
"
,
SDL_GetError
());
SDL_DestroyRenderer
(
rend
);
SDL_DestroyWindow
(
win
);
SDL_Quit
();
return
1
;
}
SDL_Surface
*
man
=
IMG_Load
(
"ressource/man.png"
);
if
(
!
man
)
{
printf
(
"error creating surface
\n
"
);
SDL_DestroyRenderer
(
rend
);
SDL_DestroyWindow
(
win
);
SDL_Quit
();
return
1
;
}
// load the image data into the graphics hardware's memory
SDL_Texture
*
tex2
=
SDL_CreateTextureFromSurface
(
rend
,
man
);
SDL_FreeSurface
(
man
);
if
(
!
tex2
)
{
printf
(
"error creating texture: %s
\n
"
,
SDL_GetError
());
SDL_DestroyRenderer
(
rend
);
SDL_DestroyWindow
(
win
);
SDL_Quit
();
return
1
;
}
// struct to hold the position and size of the sprite
SDL_Rect
dest
;
// get and scale the dimensions of texture
SDL_QueryTexture
(
tex2
,
NULL
,
NULL
,
&
dest
.
w
,
&
dest
.
h
);
dest
.
w
/=
8
;
dest
.
h
/=
8
;
// start sprite in center of screen
float
x_pos
=
70
;
float
y_pos
=
70
;
float
x_vel
=
0
;
float
y_vel
=
0
;
// keep track of which inputs are given
int
up
=
0
;
int
down
=
0
;
int
left
=
0
;
int
right
=
0
;
// set to 1 when window close button is pressed
int
close_requested
=
0
;
// animation loop
while
(
!
close_requested
)
{
// process events
SDL_Event
event
;
while
(
SDL_PollEvent
(
&
event
))
{
switch
(
event
.
type
)
{
case
SDL_QUIT
:
close_requested
=
1
;
break
;
case
SDL_KEYDOWN
:
switch
(
event
.
key
.
keysym
.
scancode
)
{
case
SDL_SCANCODE_W
:
case
SDL_SCANCODE_UP
:
up
=
1
;
break
;
case
SDL_SCANCODE_A
:
case
SDL_SCANCODE_LEFT
:
left
=
1
;
break
;
case
SDL_SCANCODE_S
:
case
SDL_SCANCODE_DOWN
:
down
=
1
;
break
;
case
SDL_SCANCODE_D
:
case
SDL_SCANCODE_RIGHT
:
right
=
1
;
break
;
}
break
;
case
SDL_KEYUP
:
switch
(
event
.
key
.
keysym
.
scancode
)
{
case
SDL_SCANCODE_W
:
case
SDL_SCANCODE_UP
:
up
=
0
;
break
;
case
SDL_SCANCODE_A
:
case
SDL_SCANCODE_LEFT
:
left
=
0
;
break
;
case
SDL_SCANCODE_S
:
case
SDL_SCANCODE_DOWN
:
down
=
0
;
break
;
case
SDL_SCANCODE_D
:
case
SDL_SCANCODE_RIGHT
:
right
=
0
;
break
;
}
break
;
}
}
// determine velocity
x_vel
=
y_vel
=
0
;
if
(
up
&&
!
down
)
y_vel
=
-
SPEED
;
if
(
down
&&
!
up
)
y_vel
=
SPEED
;
if
(
left
&&
!
right
)
x_vel
=
-
SPEED
;
if
(
right
&&
!
left
)
x_vel
=
SPEED
;
// update positions
x_pos
+=
x_vel
/
60
;
y_pos
+=
y_vel
/
60
;
// collision detection with bounds
if
(
x_pos
<=
0
)
x_pos
=
0
;
if
(
y_pos
<=
0
)
y_pos
=
0
;
if
(
x_pos
>=
WINDOW_WIDTH
-
dest
.
w
)
x_pos
=
WINDOW_WIDTH
-
dest
.
w
;
if
(
y_pos
>=
WINDOW_HEIGHT
-
dest
.
h
)
y_pos
=
WINDOW_HEIGHT
-
dest
.
h
;
// set the positions in the struct
dest
.
y
=
(
int
)
y_pos
;
dest
.
x
=
(
int
)
x_pos
;
// clear the window
SDL_RenderClear
(
rend
);
// draw the image to the window
SDL_RenderCopy
(
rend
,
tex
,
NULL
,
NULL
);
SDL_RenderCopy
(
rend
,
tex2
,
NULL
,
&
dest
);
SDL_RenderPresent
(
rend
);
// wait 1/60th of a second
SDL_Delay
(
1000
/
60
);
}
// clean up resources before exiting
SDL_DestroyTexture
(
tex
);
SDL_DestroyRenderer
(
rend
);
SDL_DestroyWindow
(
win
);
SDL_Quit
();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment