Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sfml-demo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Filip Strömbäck
sfml-demo
Commits
95a847c8
Commit
95a847c8
authored
2 years ago
by
Filip Strömbäck
Browse files
Options
Downloads
Patches
Plain Diff
Updated introduction a bit for 2022.
parent
d5367753
No related branches found
No related tags found
No related merge requests found
Pipeline
#82361
passed
2 years ago
Stage: auto
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
basic/player.png
+0
-0
0 additions, 0 deletions
basic/player.png
basic/step05.cpp
+1
-0
1 addition, 0 deletions
basic/step05.cpp
basic/step06.cpp
+1
-1
1 addition, 1 deletion
basic/step06.cpp
basic/step07.cpp
+120
-0
120 additions, 0 deletions
basic/step07.cpp
basic/step08.cpp
+74
-0
74 additions, 0 deletions
basic/step08.cpp
with
196 additions
and
1 deletion
basic/player.png
0 → 100644
+
0
−
0
View file @
95a847c8
2.69 KiB
This diff is collapsed.
Click to expand it.
basic/step05.cpp
+
1
−
0
View file @
95a847c8
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
* Fundera på:
* Fundera på:
* - Vad är skillnaden här jämfört med förra?
* - Vad är skillnaden här jämfört med förra?
* - Hur påverkas hastigheten av olika hastighet på programmet?
* - Hur påverkas hastigheten av olika hastighet på programmet?
* - Hur fungerar detta med avseende på om programmet har fokus eller ej?
*/
*/
...
...
This diff is collapsed.
Click to expand it.
basic/step06.cpp
+
1
−
1
View file @
95a847c8
...
@@ -57,7 +57,7 @@ int main() {
...
@@ -57,7 +57,7 @@ int main() {
auto
delta
=
clock
.
restart
();
auto
delta
=
clock
.
restart
();
{
{
float
distance
=
250.0
f
*
(
delta
.
as
Micros
econds
()
/
1000000.0
f
)
;
float
distance
=
250.0
f
*
delta
.
as
S
econds
();
location
+=
direction
*
distance
;
location
+=
direction
*
distance
;
}
}
...
...
This diff is collapsed.
Click to expand it.
basic/step07.cpp
0 → 100644
+
120
−
0
View file @
95a847c8
#include
<SFML/Graphics.hpp>
#include
"point.h"
#include
"standard.h"
/**
* Steg 7: Alternativ hantering av tangentbordsinmatning.
*
* Fundera på:
* - Vad är fördelar och nackdelar gentemot förra lösningen?
* - Finns det problem med den här lösningen?
*/
const
size_t
width
=
1024
;
const
size_t
height
=
768
;
class
Key_State
{
public:
Key_State
()
:
up
{
false
},
down
{
false
},
left
{
false
},
right
{
false
}
{}
void
onKey
(
sf
::
Keyboard
::
Key
key
,
bool
pressed
)
{
switch
(
key
)
{
case
sf
::
Keyboard
::
W
:
case
sf
::
Keyboard
::
Up
:
up
=
pressed
;
break
;
case
sf
::
Keyboard
::
S
:
case
sf
::
Keyboard
::
Down
:
down
=
pressed
;
break
;
case
sf
::
Keyboard
::
A
:
case
sf
::
Keyboard
::
Left
:
left
=
pressed
;
break
;
case
sf
::
Keyboard
::
D
:
case
sf
::
Keyboard
::
Right
:
right
=
pressed
;
break
;
default:
break
;
}
}
sf
::
Vector2f
direction
()
const
{
sf
::
Vector2f
result
;
if
(
up
)
result
.
y
-=
1
;
if
(
down
)
result
.
y
+=
1
;
if
(
left
)
result
.
x
-=
1
;
if
(
right
)
result
.
x
+=
1
;
return
normalize
(
result
);
}
private
:
bool
up
;
bool
down
;
bool
left
;
bool
right
;
};
int
main
()
{
sf
::
RenderWindow
window
{
sf
::
VideoMode
{
width
,
height
},
"Demo"
};
window
.
setKeyRepeatEnabled
(
false
);
window
.
setVerticalSyncEnabled
(
true
);
sf
::
CircleShape
circle
{
40
};
sf
::
Vector2f
location
{
300
,
300
};
sf
::
Clock
clock
;
Key_State
keys
;
bool
quit
=
false
;
while
(
!
quit
)
{
sf
::
Event
event
;
while
(
window
.
pollEvent
(
event
))
{
switch
(
event
.
type
)
{
case
sf
::
Event
::
Closed
:
quit
=
true
;
break
;
case
sf
::
Event
::
KeyPressed
:
keys
.
onKey
(
event
.
key
.
code
,
true
);
break
;
case
sf
::
Event
::
KeyReleased
:
keys
.
onKey
(
event
.
key
.
code
,
false
);
break
;
default:
break
;
}
}
if
(
quit
)
break
;
sf
::
Vector2f
direction
=
keys
.
direction
();
auto
delta
=
clock
.
restart
();
{
float
distance
=
250.0
f
*
delta
.
asSeconds
();
location
+=
direction
*
distance
;
}
window
.
clear
();
circle
.
setPosition
(
location
);
window
.
draw
(
circle
);
window
.
display
();
}
return
0
;
}
This diff is collapsed.
Click to expand it.
basic/step08.cpp
0 → 100644
+
74
−
0
View file @
95a847c8
#include
<SFML/Graphics.hpp>
#include
"point.h"
#include
"standard.h"
/**
* Steg 8: Använda texturer.
*/
const
size_t
width
=
1024
;
const
size_t
height
=
768
;
sf
::
Vector2f
find_direction
()
{
sf
::
Vector2f
direction
;
if
(
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
W
)
||
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
Up
))
direction
.
y
-=
1
;
if
(
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
S
)
||
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
Down
))
direction
.
y
+=
1
;
if
(
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
A
)
||
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
Left
))
direction
.
x
-=
1
;
if
(
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
D
)
||
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
Right
))
direction
.
x
+=
1
;
return
normalize
(
direction
);
}
int
main
()
{
sf
::
RenderWindow
window
{
sf
::
VideoMode
{
width
,
height
},
"Demo"
};
window
.
setKeyRepeatEnabled
(
false
);
window
.
setVerticalSyncEnabled
(
true
);
sf
::
Texture
texture
;
texture
.
loadFromFile
(
"player.png"
);
sf
::
Vector2f
textureSize
{
texture
.
getSize
()};
sf
::
RectangleShape
player
{
textureSize
};
player
.
setTexture
(
&
texture
);
player
.
setOrigin
(
textureSize
/
2.0
f
);
sf
::
Vector2f
location
{
300
,
300
};
sf
::
Clock
clock
;
bool
quit
=
false
;
while
(
!
quit
)
{
sf
::
Event
event
;
while
(
window
.
pollEvent
(
event
))
{
switch
(
event
.
type
)
{
case
sf
::
Event
::
Closed
:
quit
=
true
;
break
;
default:
break
;
}
}
if
(
quit
)
break
;
sf
::
Vector2f
direction
=
find_direction
();
auto
delta
=
clock
.
restart
();
{
float
distance
=
250.0
f
*
delta
.
asSeconds
();
location
+=
direction
*
distance
;
}
window
.
clear
();
player
.
setPosition
(
location
);
window
.
draw
(
player
);
window
.
display
();
}
return
0
;
}
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