Skip to content

Commit 08743e1

Browse files
committed
integrate with SDL2
1 parent 76f20fd commit 08743e1

File tree

1,271 files changed

+438224
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,271 files changed

+438224
-26
lines changed

CMakeLists.txt

+22-22
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@ cmake_minimum_required(VERSION 3.12.2)
22

33
project(OpenGL-tutorial)
44

5-
file(GLOB HEAD_FILES ${PROJECT_SOURCE_DIR}/include/*.h)
6-
75
set(CMAKE_CXX_FLAGS "-g -Werror -Wall -Wextra")
86
set(CMAKE_CXX_FLAGS "-std=c++17")
97
set(CMAKE_CXX_FLAGS "-g -pg -O3")
108

11-
# set(SOURCES
12-
# src/tga.cpp
13-
# src/main.cpp
14-
# )
15-
16-
# add_executable(main ${SOURCES})
9+
SET(SDL_PREFIX "${CMAKE_BINARY_DIR}/thir-part/SDL2")
1710

18-
# target_include_directories(main
19-
# PRIVATE
20-
# ${PROJECT_SOURCE_DIR}/include
21-
# )
11+
include(ExternalProject)
12+
ExternalProject_Add(SDL
13+
SOURCE_DIR ${CMAKE_SOURCE_DIR}/third-part/SDL2-2.0.9
14+
CONFIGURE_COMMAND ${CMAKE_SOURCE_DIR}/third-part/SDL2-2.0.9/configure --prefix=${SDL_PREFIX}
15+
BUILD_COMMAND make
16+
)
2217

18+
SET(SDL_LIB_DIR "${SDL_PREFIX}/lib")
19+
SET(SDL_INT_DIR "${SDL_PREFIX}/include")
20+
MESSAGE(STATUS "sdl_lib_dir ---- ${SDL_LIB_DIR}")
21+
SET(SDL_LIB "${SDL_LIB_DIR}/libSDL2-2.0.0.dylib")
2322

24-
add_library(tga_lib STATIC
23+
SET(SOURCES
2524
src/tga.cpp
25+
src/main.cpp
2626
)
2727

28-
target_include_directories(tga_lib
29-
PUBLIC
30-
${PROJECT_SOURCE_DIR}/include
31-
)
28+
INCLUDE_DIRECTORIES(include ${sdl_inc_dir})
3229

33-
add_executable(main
34-
src/main.cpp
35-
)
30+
ADD_EXECUTABLE(game ${SOURCES})
31+
32+
ADD_DEPENDENCIES(game SDL)
3633

37-
target_link_libraries(main PRIVATE
38-
tga_lib
34+
TARGET_LINK_LIBRARIES(game ${SDL_LIB})
35+
36+
TARGET_INCLUDE_DIRECTORIES(game
37+
PRIVATE
38+
${PROJECT_SOURCE_DIR}/include
3939
)

include/window.h

Whitespace-only changes.

src/main.cpp

+131-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "SDL2/SDL.h"
12
#include "tga.h"
23

34
const TGAColor white = TGAColor(255, 255, 255, 255);
@@ -7,11 +8,12 @@ void aliased_line();
78

89
void anti_aliased_line();
910

11+
int sdl();
12+
1013
int main(int argc, char** argv) {
11-
char* a = "test";
12-
aliased_line();
13-
anti_aliased_line();
14-
return 0;
14+
// aliased_line();
15+
// anti_aliased_line();
16+
return sdl();
1517
}
1618

1719
void aliased_line() {
@@ -26,4 +28,129 @@ void anti_aliased_line() {
2628
image.anti_aliasing_line(50.0, 10.0, 60.0, 90.0, red);
2729
// image.flip_horizontally();
2830
image.write_tga_file("output_anti_aliased.tga");
31+
}
32+
33+
// SDL stuff
34+
SDL_Window* pWindow = 0;
35+
SDL_Renderer* pRenderer = 0;
36+
37+
// swaps two numbers
38+
void swap(int* a, int* b) {
39+
int temp = *a;
40+
*a = *b;
41+
*b = temp;
42+
}
43+
44+
// returns absolute value of number
45+
float absolute(float x) {
46+
if (x < 0)
47+
return -x;
48+
else
49+
return x;
50+
}
51+
52+
// returns integer part of a floating point number
53+
int iPartOfNumber(float x) { return (int)x; }
54+
55+
// rounds off a number
56+
int roundNumber(float x) { return iPartOfNumber(x + 0.5); }
57+
58+
// returns fractional part of a number
59+
float fPartOfNumber(float x) {
60+
if (x > 0)
61+
return x - iPartOfNumber(x);
62+
else
63+
return x - (iPartOfNumber(x) + 1);
64+
}
65+
66+
// returns 1 - fractional part of number
67+
float rfPartOfNumber(float x) { return 1 - fPartOfNumber(x); }
68+
69+
// draws a pixel on screen of given brightness
70+
// 0<=brightness<=1. We can use your own library
71+
// to draw on screen
72+
void drawPixel(int x, int y, float brightness) {
73+
int c = 255 * brightness;
74+
SDL_SetRenderDrawColor(pRenderer, c, c, c, 255);
75+
SDL_RenderDrawPoint(pRenderer, x, y);
76+
}
77+
78+
void drawAALine(int x0, int y0, int x1, int y1) {
79+
int steep = absolute(y1 - y0) > absolute(x1 - x0);
80+
81+
// swap the co-ordinates if slope > 1 or we
82+
// draw backwards
83+
if (steep) {
84+
swap(&x0, &y0);
85+
swap(&x1, &y1);
86+
}
87+
if (x0 > x1) {
88+
swap(&x0, &x1);
89+
swap(&y0, &y1);
90+
}
91+
92+
// compute the slope
93+
float dx = x1 - x0;
94+
float dy = y1 - y0;
95+
float gradient = dy / dx;
96+
if (dx == 0.0) gradient = 1;
97+
98+
int xpxl1 = x0;
99+
int xpxl2 = x1;
100+
float intersectY = y0;
101+
102+
// main loop
103+
if (steep) {
104+
int x;
105+
for (x = xpxl1; x <= xpxl2; x++) {
106+
// pixel coverage is determined by fractional
107+
// part of y co-ordinate
108+
drawPixel(iPartOfNumber(intersectY), x, rfPartOfNumber(intersectY));
109+
drawPixel(iPartOfNumber(intersectY) - 1, x, fPartOfNumber(intersectY));
110+
intersectY += gradient;
111+
}
112+
} else {
113+
int x;
114+
for (x = xpxl1; x <= xpxl2; x++) {
115+
// pixel coverage is determined by fractional
116+
// part of y co-ordinate
117+
drawPixel(x, iPartOfNumber(intersectY), rfPartOfNumber(intersectY));
118+
drawPixel(x, iPartOfNumber(intersectY) - 1, fPartOfNumber(intersectY));
119+
intersectY += gradient;
120+
}
121+
}
122+
}
123+
124+
int sdl() {
125+
SDL_Event event;
126+
127+
// initialize SDL
128+
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0) {
129+
// if succeeded create our window
130+
pWindow =
131+
SDL_CreateWindow("Anti-Aliased Line ", SDL_WINDOWPOS_CENTERED,
132+
SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
133+
134+
// if the window creation succeeded create our renderer
135+
if (pWindow != 0) pRenderer = SDL_CreateRenderer(pWindow, -1, 0);
136+
} else
137+
return 1; // sdl could not initialize
138+
139+
while (1) {
140+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT) break;
141+
142+
// Sets background color to white
143+
SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255);
144+
SDL_RenderClear(pRenderer);
145+
146+
// draws a black AALine
147+
drawAALine(80, 200, 550, 150);
148+
149+
// show the window
150+
SDL_RenderPresent(pRenderer);
151+
}
152+
153+
// clean up SDL
154+
SDL_Quit();
155+
return 0;
29156
}

src/tga.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <algorithm>
66
#include <fstream>
77
#include <iostream>
8+
#include "SDL2/SDL.h"
89

910
TGAImage::TGAImage() : data(NULL), width(0), height(0), bytespp(0) {}
1011

third-part/SDL2-2.0.9.tar.gz

5 MB
Binary file not shown.

third-part/SDL2-2.0.9/Android.mk

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
###########################
4+
#
5+
# SDL shared library
6+
#
7+
###########################
8+
9+
include $(CLEAR_VARS)
10+
11+
LOCAL_MODULE := SDL2
12+
13+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
14+
15+
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
16+
17+
LOCAL_SRC_FILES := \
18+
$(subst $(LOCAL_PATH)/,, \
19+
$(wildcard $(LOCAL_PATH)/src/*.c) \
20+
$(wildcard $(LOCAL_PATH)/src/audio/*.c) \
21+
$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \
22+
$(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) \
23+
$(LOCAL_PATH)/src/atomic/SDL_atomic.c.arm \
24+
$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \
25+
$(wildcard $(LOCAL_PATH)/src/core/android/*.c) \
26+
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
27+
$(wildcard $(LOCAL_PATH)/src/dynapi/*.c) \
28+
$(wildcard $(LOCAL_PATH)/src/events/*.c) \
29+
$(wildcard $(LOCAL_PATH)/src/file/*.c) \
30+
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
31+
$(wildcard $(LOCAL_PATH)/src/haptic/android/*.c) \
32+
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \
33+
$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \
34+
$(wildcard $(LOCAL_PATH)/src/joystick/hidapi/*.c) \
35+
$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) \
36+
$(wildcard $(LOCAL_PATH)/src/power/*.c) \
37+
$(wildcard $(LOCAL_PATH)/src/power/android/*.c) \
38+
$(wildcard $(LOCAL_PATH)/src/filesystem/android/*.c) \
39+
$(wildcard $(LOCAL_PATH)/src/sensor/*.c) \
40+
$(wildcard $(LOCAL_PATH)/src/sensor/android/*.c) \
41+
$(wildcard $(LOCAL_PATH)/src/render/*.c) \
42+
$(wildcard $(LOCAL_PATH)/src/render/*/*.c) \
43+
$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \
44+
$(wildcard $(LOCAL_PATH)/src/thread/*.c) \
45+
$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \
46+
$(wildcard $(LOCAL_PATH)/src/timer/*.c) \
47+
$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \
48+
$(wildcard $(LOCAL_PATH)/src/video/*.c) \
49+
$(wildcard $(LOCAL_PATH)/src/video/android/*.c) \
50+
$(wildcard $(LOCAL_PATH)/src/video/yuv2rgb/*.c) \
51+
$(wildcard $(LOCAL_PATH)/src/test/*.c))
52+
53+
LOCAL_SHARED_LIBRARIES := hidapi
54+
55+
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
56+
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
57+
58+
ifeq ($(NDK_DEBUG),1)
59+
cmd-strip :=
60+
endif
61+
62+
include $(BUILD_SHARED_LIBRARY)
63+
64+
###########################
65+
#
66+
# SDL static library
67+
#
68+
###########################
69+
70+
LOCAL_MODULE := SDL2_static
71+
72+
LOCAL_MODULE_FILENAME := libSDL2
73+
74+
LOCAL_LDLIBS :=
75+
LOCAL_EXPORT_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
76+
77+
include $(BUILD_STATIC_LIBRARY)
78+
79+
###########################
80+
#
81+
# SDL main static library
82+
#
83+
###########################
84+
85+
include $(CLEAR_VARS)
86+
87+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
88+
89+
LOCAL_MODULE := SDL2_main
90+
91+
LOCAL_MODULE_FILENAME := libSDL2main
92+
93+
include $(BUILD_STATIC_LIBRARY)
94+
95+
###########################
96+
#
97+
# hidapi library
98+
#
99+
###########################
100+
101+
include $(CLEAR_VARS)
102+
103+
LOCAL_CPPFLAGS += -std=c++11
104+
105+
LOCAL_SRC_FILES := src/hidapi/android/hid.cpp
106+
107+
LOCAL_MODULE := libhidapi
108+
LOCAL_LDLIBS := -llog
109+
110+
include $(BUILD_SHARED_LIBRARY)

third-part/SDL2-2.0.9/BUGS.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Bugs are now managed in the SDL bug tracker, here:
3+
4+
https://bugzilla.libsdl.org/
5+
6+
You may report bugs there, and search to see if a given issue has already
7+
been reported, discussed, and maybe even fixed.
8+
9+
10+
You may also find help at the SDL forums/mailing list:
11+
12+
https://discourse.libsdl.org/
13+
14+
Bug reports are welcome here, but we really appreciate if you use Bugzilla, as
15+
bugs discussed on the mailing list may be forgotten or missed.
16+

0 commit comments

Comments
 (0)