Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
assignment_2023
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
Benedict Gaster
assignment_2023
Commits
3804b57d
Commit
3804b57d
authored
4 years ago
by
BG Dummy
Browse files
Options
Downloads
Patches
Plain Diff
added beginnings of context
parent
5e75dc11
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
include/graphics.hpp
+38
-0
38 additions, 0 deletions
include/graphics.hpp
src/graphics.cpp
+133
-0
133 additions, 0 deletions
src/graphics.cpp
with
172 additions
and
0 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
3804b57d
a.out
\ No newline at end of file
This diff is collapsed.
Click to expand it.
include/graphics.hpp
0 → 100644
+
38
−
0
View file @
3804b57d
/**
*
* \author Benedict R. Gaster
*/
#pragma once
#include
<vector>
#include
<SDL2/SDL.h>
namespace
uwe
{
class
Context
{
public:
/** \brief Create a context
* \param width an integer setting the window width
* \param height an integer setting the window height
*/
Context
(
int
width
,
int
height
);
~
Context
();
/// Output details of GPU backend and texture formats
void
dump_renderer_info
();
/// run the control loop, returns only when user exits
void
run
();
private:
int
height_
;
///< window height
int
width_
;
///< window width
int
tex_width_
;
///< texture width
int
tex_height_
;
///< texture height
SDL_Window
*
window_
;
///< window handle
SDL_Renderer
*
renderer_
;
///< graphics backend handle
SDL_Texture
*
texture_
;
///< pixel buffer texture handle
std
::
vector
<
uint8_t
>
pixels_
;
///< pixels representing framebuffer
};
}
This diff is collapsed.
Click to expand it.
src/graphics.cpp
0 → 100644
+
133
−
0
View file @
3804b57d
#include
<iostream>
#include
<graphics.hpp>
#include
<iomanip>
#include
<cstring>
namespace
uwe
{
Context
::
Context
(
int
width
,
int
height
)
:
width_
{
width
},
height_
{
height
},
tex_width_
{
width
},
tex_height_
{
height
},
pixels_
(
tex_width_
*
tex_height_
*
4
,
0
)
{
SDL_Init
(
SDL_INIT_EVERYTHING
);
window_
=
SDL_CreateWindow
(
"SDL2"
,
SDL_WINDOWPOS_UNDEFINED
,
SDL_WINDOWPOS_UNDEFINED
,
width_
,
height_
,
SDL_WINDOW_SHOWN
);
renderer_
=
SDL_CreateRenderer
(
window_
,
-
1
,
SDL_RENDERER_ACCELERATED
);
texture_
=
SDL_CreateTexture
(
renderer_
,
SDL_PIXELFORMAT_ARGB8888
,
SDL_TEXTUREACCESS_STREAMING
,
tex_width_
,
tex_height_
);
}
Context
::~
Context
()
{
SDL_DestroyRenderer
(
renderer_
);
SDL_DestroyWindow
(
window_
);
SDL_Quit
();
}
void
Context
::
run
()
{
SDL_Event
event
;
bool
running
=
true
;
bool
useLocktexture
=
false
;
unsigned
int
frames
=
0
;
Uint64
start
=
SDL_GetPerformanceCounter
();
while
(
running
)
{
SDL_SetRenderDrawColor
(
renderer_
,
0
,
0
,
0
,
SDL_ALPHA_OPAQUE
);
SDL_RenderClear
(
renderer_
);
while
(
SDL_PollEvent
(
&
event
)
)
{
if
(
(
SDL_QUIT
==
event
.
type
)
||
(
SDL_KEYDOWN
==
event
.
type
&&
SDL_SCANCODE_ESCAPE
==
event
.
key
.
keysym
.
scancode
)
)
{
running
=
false
;
break
;
}
if
(
SDL_KEYDOWN
==
event
.
type
&&
SDL_SCANCODE_L
==
event
.
key
.
keysym
.
scancode
)
{
useLocktexture
=
!
useLocktexture
;
std
::
cout
<<
"Using "
<<
(
useLocktexture
?
"SDL_LockTexture() + memcpy()"
:
"SDL_UpdateTexture()"
)
<<
std
::
endl
;
}
}
// splat down some random pixels
for
(
unsigned
int
i
=
0
;
i
<
1000
;
i
++
)
{
const
unsigned
int
x
=
rand
()
%
tex_width_
;
const
unsigned
int
y
=
rand
()
%
tex_height_
;
const
unsigned
int
offset
=
(
tex_width_
*
4
*
y
)
+
x
*
4
;
pixels_
[
offset
+
0
]
=
rand
()
%
256
;
// b
pixels_
[
offset
+
1
]
=
rand
()
%
256
;
// g
pixels_
[
offset
+
2
]
=
rand
()
%
256
;
// r
pixels_
[
offset
+
3
]
=
SDL_ALPHA_OPAQUE
;
// a
}
if
(
useLocktexture
)
{
unsigned
char
*
lockedPixels
=
nullptr
;
int
pitch
=
0
;
SDL_LockTexture
(
texture_
,
NULL
,
reinterpret_cast
<
void
**
>
(
&
lockedPixels
),
&
pitch
);
std
::
memcpy
(
lockedPixels
,
pixels_
.
data
(),
pixels_
.
size
()
);
SDL_UnlockTexture
(
texture_
);
}
else
{
SDL_UpdateTexture
(
texture_
,
NULL
,
pixels_
.
data
(),
tex_width_
*
4
);
}
SDL_RenderCopy
(
renderer_
,
texture_
,
NULL
,
NULL
);
SDL_RenderPresent
(
renderer_
);
frames
++
;
const
uint64_t
end
=
SDL_GetPerformanceCounter
();
const
static
uint64_t
freq
=
SDL_GetPerformanceFrequency
();
const
double
seconds
=
(
end
-
start
)
/
static_cast
<
double
>
(
freq
);
if
(
seconds
>
2.0
)
{
std
::
cout
<<
frames
<<
" frames in "
<<
std
::
setprecision
(
1
)
<<
std
::
fixed
<<
seconds
<<
" seconds = "
<<
std
::
setprecision
(
1
)
<<
std
::
fixed
<<
frames
/
seconds
<<
" FPS ("
<<
std
::
setprecision
(
3
)
<<
std
::
fixed
<<
(
seconds
*
1000.0
)
/
frames
<<
" ms/frame)"
<<
std
::
endl
;
start
=
end
;
frames
=
0
;
}
}
}
void
Context
::
dump_renderer_info
()
{
SDL_RendererInfo
info
;
SDL_GetRendererInfo
(
renderer_
,
&
info
);
std
::
cout
<<
"Renderer name: "
<<
info
.
name
<<
std
::
endl
;
std
::
cout
<<
"Texture formats: "
<<
std
::
endl
;
for
(
uint32_t
i
=
0
;
i
<
info
.
num_texture_formats
;
i
++
)
{
std
::
cout
<<
SDL_GetPixelFormatName
(
info
.
texture_formats
[
i
]
)
<<
std
::
endl
;
}
}
}
\ No newline at end of file
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