Git Product home page Git Product logo

tsherif / space-shooter.c Goto Github PK

View Code? Open in Web Editor NEW
1.3K 22.0 74.0 98.66 MB

A cross-platform, top-down 2D space shooter written in C using only platform libraries.

Home Page: https://tsherif.github.io/space-shooter.c/site/

License: MIT License

Batchfile 0.11% C 99.13% Makefile 0.25% GLSL 0.16% HTML 0.35%
c game-development opengl emscripten linux wasm webassembly win32 cross-platform gamedev

space-shooter.c's Introduction

space-shooter.c

A cross-platform, top-down 2D space shooter written in C using only platform libraries.

gif

Dependencies

  • Base: Win32 (Windows), Xlib (Linux), Emscripten (Web)
  • Rendering: OpenGL 3.3 (Windows/Linux), OpenGL ES 3.0 (Web)
  • Audio: XAudio2 (Windows), ALSA/pthread (Linux), OpenAL (Web)
  • Gamepad: XInput (Windows), evdev (Linux), Emscripten (Web)

Description

space-shooter.c is a cross-platform, top-down 2D space shooter written in standard C11 using only platform libraries (with platform libraries defined as anything included in the C standard library or supported platforms). space-shooter.c has been tested on Windows 10, Ubuntu Linux 16.04, Chrome 103 and Firefox 103. This project drew heavy inspiration from Handmade Hero and pacman.c.

The design and architecture of space-shooter.c is described here.

Caveat

I am not a professional game developer, nor a professional C programmer, so the design may be unconventional or sub-optimal in many ways. Happy to take feedback from any pros out there!

Building

Windows

  • In a shell with cl set up, run build.bat for a debug build or build.bat release for an optimized build.
  • Run space-shooter.exe from the build/ directory.

Linux

  • Make sure development headers for the Linux kernel, Xlib, glx and ALSA are installed.
    • E.g. on Ubuntu, run the following: sudo apt install linux-libc-dev libx11-dev mesa-common-dev libasound2-dev
  • Run make linux for a debug build or make linux-release for an optimized build.
  • Run ./space-shooter from the build/ directory.

Web

  • Make sure make and emscripten are installed.
  • Run make web for a debug build or make web-release for an optimized build.
  • Serve the build/ directory for debug builds or the site/ directory for release builds from a local web server (e.g. python -m SimpleHTTPServer), and open the page in Chrome or Firefox.

What About Mac?

I hope to work on a Mac platform layer as soon as I have a Mac machine to work on.

Contributing

Since this project is intended as a personal challenge, I am not accepting external contributions at this time. I am very open to feedback, however, so feel free to open an issue for anything that needs fixing.

Asset Credits

space-shooter.c's People

Contributors

tsherif avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

space-shooter.c's Issues

Test for asset directory presence

Related to #3

Some people like to move the executable out of the repo without realizing that the asset directory needs to be moved too. Right now, this results in a cryptic error message about the renderer failing to initialize. Check if asset directory is present and print a useful error message if not.

Check for GPU errors after initialization.

The only check currently performed is for shader compilation. I should probably have at least a glGetError after all initialization to make sure resources were allocated properly.

windows-audio.c compilation error with VS2019, VS2022

C:\git\space-shooter>build.bat
assets\audio\Explode1.wav
assets\audio\Hit_Hurt2.wav
assets\audio\Jump1.wav
assets\audio\Laser_002.wav
assets\audio\music.wav
assets\shaders\fs.glsl
assets\shaders\vs.glsl
assets\sprites\enemy-big.bmp
assets\sprites\enemy-medium.bmp
assets\sprites\enemy-small.bmp
assets\sprites\explosion.bmp
assets\sprites\laser-bolts.bmp
assets\sprites\pixelspritefont32.bmp
assets\sprites\ship.bmp
14 File(s) copied

C:\git\space-shooter\build>cl /std:c11 /W4 /WX /wd5105 /wd4201 /wd4100 /D SOGL_MAJOR_VERSION=3 /D SOGL_MINOR_VERSION=3 /Fespace-shooter /Zi /D SPACE_SHOOTER_DEBUG ..\src\shared\*.c ..\src\game\*.c ..\src\platform\windows\*.c user32.lib gdi32.lib opengl32.lib xinput.lib ole32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

data.c
entities.c
events.c
game.c
renderer.c
sprites.c
utils.c
windows-audio.c
..\src\platform\windows\windows-audio.c(81): error C2440: 'initializing': cannot convert from 'void (__cdecl *)(IXAudio2VoiceCallback *,UINT32)' to 'void (__stdcall *)(IXAudio2VoiceCallback *,UINT32)'
..\src\platform\windows\windows-audio.c(81): error C2440: 'initializing': cannot convert from 'void (__cdecl *)(IXAudio2VoiceCallback *)' to 'void (__stdcall *)(IXAudio2VoiceCallback *)'
..\src\platform\windows\windows-audio.c(81): error C2440: 'initializing': cannot convert from 'void (__cdecl *)(IXAudio2VoiceCallback *,void *)' to 'void (__stdcall *)(IXAudio2VoiceCallback *,void *)'
..\src\platform\windows\windows-audio.c(81): error C2440: 'initializing': cannot convert from 'void (__cdecl *)(IXAudio2VoiceCallback *,void *,HRESULT)' to 'void (__stdcall *)(IXAudio2VoiceCallback *,void *,HRESULT)'
windows.c
Generating Code...
C:\git\space-shooter>dir build


 Directory of C:\git\space-shooter\build

12/12/2021  01:41 PM    <DIR>          .
12/12/2021  01:41 PM    <DIR>          ..
12/12/2021  01:41 PM    <DIR>          assets
12/12/2021  01:41 PM             2,962 data.obj
12/12/2021  01:41 PM             8,044 entities.obj
12/12/2021  01:41 PM             7,104 events.obj
12/12/2021  01:41 PM           190,256 game.obj
12/12/2021  01:41 PM            30,857 renderer.obj
12/12/2021  01:41 PM            25,528 sprites.obj
12/12/2021  01:41 PM            12,625 utils.obj
12/12/2021  01:41 PM           118,784 vc140.pdb
12/12/2021  01:41 PM           154,138 windows.obj

'SPACE_SHOOTER_MIN_FRAME_TIME' is undeclared identifier

The windows.c needs SPACE_SHOOTER_MIN_FRAME_TIME but the constants.h define it as SPACE_SHOOTER_MIN_FRAME_TIME_NS. I also tried to remove the _NS part but then the game just freeze. After looking through the startup code, I saw this:

        if (useSleep && SPACE_SHOOTER_MIN_FRAME_TIME - elapsedTime > 1.0f) {
            DWORD sleepMs = (DWORD) (SPACE_SHOOTER_MIN_FRAME_TIME - elapsedTime);
            Sleep(sleepMs);

The sleepMs was a very high number. If I replace the macro with something else like 1/60 then it would work just fine.

Not checking the 'dead' entity flag while iterating over entities.

As far as I understand, dead entities are only filtered before calling the drawing/rendering functions. While iterating over entities we don't seem to be skipping those with their ->dead[i] flag set. I am not sure if that's correct though.

In the function src/game/game.c/mainGame we call simWorld, which checks for collisions between player bullets and enemies (i.e it sets the ->dead[i] flag), later 'mainGame' also calls simPlayer which checks for collisions between the player and the enemies using the checkPlayerCollision function. But the checkPlayerCollision function doesn't skip "dead" enemies, which makes it possible for "dead" enemies to collide with the player.

Clean up error handling in main platform functions

main and WinMain in the top-level platform files currently return on error without doing any cleanup. Set up a goto chain to handle cleanup depending on which resources have been allocated (like in platform_loadFile).

Set a max refresh rate

If vsync isn't enabled, the game loop will currently run as fast as possible, which is pointless since game state updates are fixed at 60Hz. Setting the loop to max out at a high refresh rate (e.g. 300Hz) would let vsync do its thing but prevent overusing resources when it's not available.

Issue compiling on Windows - error C2220 and warning C4204

Hi Tarek, thank you for making this project!

For anyone trying to compile this project on windows (Windows 10 Home for me), I ran into a slight problem. After running build.bat in a shell with cl setup, I got an error:

....
..\src\game\entities.c(135): error C2220: the following warning is treated as an error
..\src\game\entities.c(135): warning C4204: nonstandard extension used: non-constant aggregate initializer
...More of these warnings...

The way I solved this for now was to remove the /WX compile option from the build.bat script (basically replace line 8 with the line below):

@set shared_args=/std:c11 /W4 /wd5105 /wd4201 /wd4100 /D SOGL_MAJOR_VERSION=3 /D SOGL_MINOR_VERSION=3 /Fespace-shooter

/WX tells the compiler to treat all warnings as errors, so this probably isn't the best solution! But it's a workaround for now. Im not a C/C++ dev so not sure how to solve "non-constant aggregate initializer". If I figure it out I will post here.

"error: GLSL 3.30 is not supported"

A continuation of #2. A new update came to my qemu fork, and they added support for OpenGL 4.1. However, running with MESA_GL_VERSION_OVERRIDE=3.3 build/sp* (because the version string is for some reason, still at 2.1), fails with

Program failed to link!
Vertex shader failed to compile!
0:1(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES

Fragment shader failed to compile!
0:1(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES

Gamepad polling should be time-based.

It's currently based on number of ticks, which means it will poll at a different rate on how fast the game loop is running. Set it to a constant rate of 1Hz.

Allocate buffers in renderer_init

Currently buffers are allocated on the first draw using glBufferData. Move the allocations to renderer_init and update them in game_draw using glBufferSubData.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.