Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Jan 31, 2023
0 parents commit 97faab4
Show file tree
Hide file tree
Showing 10 changed files with 621 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cat
*.o
20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"files.associations": {
"atomic": "cpp",
"*.tcc": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"string_view": "cpp",
"functional": "cpp",
"new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp"
}
}
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2023, Valentin Charbonnier
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# A simple Makefile for compiling small SDL projects

# set the compiler
CC := clang

# set the compiler flags
CFLAGS := `sdl2-config --libs --cflags` -ggdb3 -O0 --std=c99 -Wall -lSDL2_image -lX11 -lXrender -lGL -lm -lstdc++
# add header files here
HDRS := sdlx11.hpp \

# add source files here
SRCS := main.cpp \
sdlx11.cpp \

# generate names of object files
OBJS := $(SRCS:.cpp=.o)

# name of executable
EXEC := cat

# default recipe
all: $(EXEC)

showfont: showfont.c Makefile
$(CC) -o $@ $@.c $(CFLAGS) $(LIBS)

glfont: glfont.c Makefile
$(CC) -o $@ $@.c $(CFLAGS) $(LIBS)

# recipe for building the final executable
$(EXEC): $(OBJS) $(HDRS) Makefile
$(CC) -o $@ $(OBJS) $(CFLAGS)

# recipe for building object files
#$(OBJS): $(@:.o=.c) $(HDRS) Makefile
# $(CC) -o $@ $(@:.o=.c) -c $(CFLAGS)

# recipe to clean the workspace
clean:
rm -f $(EXEC) $(OBJS)

.PHONY: all clean
Binary file added cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doodle
Binary file not shown.
116 changes: 116 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "sdlx11.hpp"

enum State
{
IDLE,
RUN_LEFT,
RUN_RIGHT
};

class Cat
{
public:
Cat(SDL_Renderer *_renderer)
{
renderer = _renderer;
image = IMG_Load("cat.png");
texture = SDL_CreateTextureFromSurface(renderer, image);
state = State::IDLE;
}

~Cat()
{
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
}

void update()
{
ticks = SDL_GetTicks();
sprite = (ticks / 100) % 4;

srcrect = { sprite * 32, 0, 32, 32 };
dstrect = { 0, 0, 32, 32 };
}

void draw()
{
SDL_RenderCopy(renderer, texture, &srcrect, &dstrect);
}

private:
SDL_Renderer *renderer;

SDL_Texture *texture;
SDL_Surface *image;

int sprite;
int ticks;
SDL_Rect srcrect;
SDL_Rect dstrect;

int x;
int y;
State state;
};

class MySDLx11App : public SDLx11
{
public:
void init()
{
SDL_Create("Cat", 0, 0, 32, 32, 0, false, 1.0f);

SDL_DisplayMode dm;
if (SDL_GetDesktopDisplayMode(0, &dm) != 0)
{
SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError());
return quit();
}

SDL_SetWindowPosition(sdl_window_, dm.w / 2, dm.h - 64);
}

void run()
{
SDL_Event event;
bool done = false;

init();

Cat *cat = new Cat(renderer_);

while (!done)
{
cat->update();

while (SDL_PollEvent(&event) != NULL)
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
}
}

SDL_RenderClear(renderer_);
cat->draw();
SDL_RenderPresent(renderer_);
}

quit();
}

void quit()
{
SDL_Destroy();
}
};

int main(int argc, char** argv)
{
MySDLx11App app;
app.run();
return 0;
}
Loading

0 comments on commit 97faab4

Please sign in to comment.